Source of file CropperFieldTest.php
Size: 3,178 Bytes - Last Modified: 2021-12-23T10:59:36+00:00
/var/www/docs.ssmods.com/process/src/tests/CropperTest/CropperFieldTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 | <?php namespace CropperTest; use Form; use FieldList; use Controller; use CropperField; use CropObject; class CropperFieldTest extends TestCase { /** * The field must set its adapter's form field */ public function testSetForm() { $field = $this->createField(); $form = new Form( new Controller(), 'Test', new FieldList(), new FieldList() ); $field->setForm($form); $this->assertEquals( $form, $field->getAdapter()->getFormField()->getForm() ); } /** * Test the Injector config to ensure a Cropper is always available */ public function testGetInjectCropper() { $field = $this->createField(); $this->assertInstanceOf( 'CropperField\Cropper\CropperInterface', $field->getCropper() ); } /** * Test that canCrop returns true at the right time. * This is currently only when the field's "Enabled" value is set */ public function testCanCrop() { $field = $this->createField(); $this->assertFalse($field->canCrop()); $field->setValue(array( 'Enabled' => 'on', )); $this->assertTrue($field->canCrop()); $field->setValue(array()); $this->assertFalse($field->canCrop()); } /** * Test that getCropData returns valid JSON */ public function testGetCropData() { $field = $this->createField(); try { $field->getCropData(); $this->fail('Expected a NoCropDataException'); } catch(CropperField\CropperField_NoCropDataException $e) { } $field->setValue(array( 'Data' => 'this is not valid JSON, m8', )); try { $field->getCropData(); $this->fail('Expected an InvalidCropDataException'); } catch(CropperField\CropperField_InvalidCropDataException $e) { } $sampleCropData = array( 'x' => 30, 'y' => 60, 'width' => 100, 'height' => 200, ); $field->setValue(array( 'Data' => json_encode($sampleCropData), )); $data = $field->getCropData(); $this->assertInternalType( 'array', $data ); $this->assertEquals( $sampleCropData, $data ); } /** * Test the getRecord method's "clever" form based path */ public function testCleverGetRecord() { $cropObject = new CropObject(); $cropObject->write(); $this->setCropObject($cropObject); // Create a test form $form = $this->createForm(); $this->assertNotEquals( $cropObject, $form->getRecord(), 'Precondition failed: initially, the form should have no record' ); // Load its data, thus triggering Form's internal setRecord $form->loadDataFrom($cropObject); $this->assertEquals( $cropObject, $form->getRecord(), 'Precondition failed: loadDataFrom should set the Form record' ); $field = $this->createField(); $this->assertNull($field->getRecord()); $field->setForm($form); $this->assertEquals( $cropObject, $form->getRecord(), 'Form should provide the record if none is directly specified' ); } /** * Mundane setting and getting... */ public function testSetGetRecord() { $cropObject = new CropObject(); $cropObject->write(); $this->setCropObject($cropObject); $field = $this->createField(); $field->setRecord($cropObject); $this->assertEquals( $cropObject, $field->getRecord() ); } } |