annotate core/modules/image/tests/src/Kernel/ImageItemTest.php @ 3:e11175134f4e

Attempt to introduce editable version of theme
author Chris Cannam
date Tue, 05 Dec 2017 11:25:38 +0000
parents 4c8ae668cc8c
children 1fec387a4317
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\image\Kernel;
Chris@0 4
Chris@0 5 use Drupal\Core\Entity\EntityStorageException;
Chris@0 6 use Drupal\Core\Field\FieldItemInterface;
Chris@0 7 use Drupal\Core\Field\FieldItemListInterface;
Chris@0 8 use Drupal\Core\Field\FieldStorageDefinitionInterface;
Chris@0 9 use Drupal\entity_test\Entity\EntityTest;
Chris@0 10 use Drupal\field\Entity\FieldConfig;
Chris@0 11 use Drupal\Tests\field\Kernel\FieldKernelTestBase;
Chris@0 12 use Drupal\field\Entity\FieldStorageConfig;
Chris@0 13 use Drupal\file\Entity\File;
Chris@0 14
Chris@0 15 /**
Chris@0 16 * Tests using entity fields of the image field type.
Chris@0 17 *
Chris@0 18 * @group image
Chris@0 19 */
Chris@0 20 class ImageItemTest extends FieldKernelTestBase {
Chris@0 21
Chris@0 22 /**
Chris@0 23 * Modules to enable.
Chris@0 24 *
Chris@0 25 * @var array
Chris@0 26 */
Chris@0 27 public static $modules = ['file', 'image'];
Chris@0 28
Chris@0 29 /**
Chris@0 30 * Created file entity.
Chris@0 31 *
Chris@0 32 * @var \Drupal\file\Entity\File
Chris@0 33 */
Chris@0 34 protected $image;
Chris@0 35
Chris@0 36 /**
Chris@0 37 * @var \Drupal\Core\Image\ImageFactory
Chris@0 38 */
Chris@0 39 protected $imageFactory;
Chris@0 40
Chris@0 41 protected function setUp() {
Chris@0 42 parent::setUp();
Chris@0 43
Chris@0 44 $this->installEntitySchema('file');
Chris@0 45 $this->installSchema('file', ['file_usage']);
Chris@0 46
Chris@0 47 FieldStorageConfig::create([
Chris@0 48 'entity_type' => 'entity_test',
Chris@0 49 'field_name' => 'image_test',
Chris@0 50 'type' => 'image',
Chris@0 51 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
Chris@0 52 ])->save();
Chris@0 53 FieldConfig::create([
Chris@0 54 'entity_type' => 'entity_test',
Chris@0 55 'field_name' => 'image_test',
Chris@0 56 'bundle' => 'entity_test',
Chris@0 57 'settings' => [
Chris@0 58 'file_extensions' => 'jpg',
Chris@0 59 ],
Chris@0 60 ])->save();
Chris@0 61 file_unmanaged_copy(\Drupal::root() . '/core/misc/druplicon.png', 'public://example.jpg');
Chris@0 62 $this->image = File::create([
Chris@0 63 'uri' => 'public://example.jpg',
Chris@0 64 ]);
Chris@0 65 $this->image->save();
Chris@0 66 $this->imageFactory = $this->container->get('image.factory');
Chris@0 67 }
Chris@0 68
Chris@0 69 /**
Chris@0 70 * Tests using entity fields of the image field type.
Chris@0 71 */
Chris@0 72 public function testImageItem() {
Chris@0 73 // Create a test entity with the image field set.
Chris@0 74 $entity = EntityTest::create();
Chris@0 75 $entity->image_test->target_id = $this->image->id();
Chris@0 76 $entity->image_test->alt = $alt = $this->randomMachineName();
Chris@0 77 $entity->image_test->title = $title = $this->randomMachineName();
Chris@0 78 $entity->name->value = $this->randomMachineName();
Chris@0 79 $entity->save();
Chris@0 80
Chris@0 81 $entity = EntityTest::load($entity->id());
Chris@0 82 $this->assertTrue($entity->image_test instanceof FieldItemListInterface, 'Field implements interface.');
Chris@0 83 $this->assertTrue($entity->image_test[0] instanceof FieldItemInterface, 'Field item implements interface.');
Chris@0 84 $this->assertEqual($entity->image_test->target_id, $this->image->id());
Chris@0 85 $this->assertEqual($entity->image_test->alt, $alt);
Chris@0 86 $this->assertEqual($entity->image_test->title, $title);
Chris@0 87 $image = $this->imageFactory->get('public://example.jpg');
Chris@0 88 $this->assertEqual($entity->image_test->width, $image->getWidth());
Chris@0 89 $this->assertEqual($entity->image_test->height, $image->getHeight());
Chris@0 90 $this->assertEqual($entity->image_test->entity->id(), $this->image->id());
Chris@0 91 $this->assertEqual($entity->image_test->entity->uuid(), $this->image->uuid());
Chris@0 92
Chris@0 93 // Make sure the computed entity reflects updates to the referenced file.
Chris@0 94 file_unmanaged_copy(\Drupal::root() . '/core/misc/druplicon.png', 'public://example-2.jpg');
Chris@0 95 $image2 = File::create([
Chris@0 96 'uri' => 'public://example-2.jpg',
Chris@0 97 ]);
Chris@0 98 $image2->save();
Chris@0 99
Chris@0 100 $entity->image_test->target_id = $image2->id();
Chris@0 101 $entity->image_test->alt = $new_alt = $this->randomMachineName();
Chris@0 102 // The width and height is only updated when width is not set.
Chris@0 103 $entity->image_test->width = NULL;
Chris@0 104 $entity->save();
Chris@0 105 $this->assertEqual($entity->image_test->entity->id(), $image2->id());
Chris@0 106 $this->assertEqual($entity->image_test->entity->getFileUri(), $image2->getFileUri());
Chris@0 107 $image = $this->imageFactory->get('public://example-2.jpg');
Chris@0 108 $this->assertEqual($entity->image_test->width, $image->getWidth());
Chris@0 109 $this->assertEqual($entity->image_test->height, $image->getHeight());
Chris@0 110 $this->assertEqual($entity->image_test->alt, $new_alt);
Chris@0 111
Chris@0 112 // Check that the image item can be set to the referenced file directly.
Chris@0 113 $entity->image_test = $this->image;
Chris@0 114 $this->assertEqual($entity->image_test->target_id, $this->image->id());
Chris@0 115
Chris@0 116 // Delete the image and try to save the entity again.
Chris@0 117 $this->image->delete();
Chris@0 118 $entity = EntityTest::create(['mame' => $this->randomMachineName()]);
Chris@0 119 $entity->save();
Chris@0 120
Chris@0 121 // Test image item properties.
Chris@0 122 $expected = ['target_id', 'entity', 'alt', 'title', 'width', 'height'];
Chris@0 123 $properties = $entity->getFieldDefinition('image_test')->getFieldStorageDefinition()->getPropertyDefinitions();
Chris@0 124 $this->assertEqual(array_keys($properties), $expected);
Chris@0 125
Chris@0 126 // Test the generateSampleValue() method.
Chris@0 127 $entity = EntityTest::create();
Chris@0 128 $entity->image_test->generateSampleItems();
Chris@0 129 $this->entityValidateAndSave($entity);
Chris@0 130 $this->assertEqual($entity->image_test->entity->get('filemime')->value, 'image/jpeg');
Chris@0 131 }
Chris@0 132
Chris@0 133 /**
Chris@0 134 * Tests a malformed image.
Chris@0 135 */
Chris@0 136 public function testImageItemMalformed() {
Chris@0 137 // Validate entity is an image and don't gather dimensions if it is not.
Chris@0 138 $entity = EntityTest::create();
Chris@0 139 $entity->image_test = NULL;
Chris@0 140 $entity->image_test->target_id = 9999;
Chris@0 141 // PHPUnit re-throws E_USER_WARNING as an exception.
Chris@0 142 try {
Chris@0 143 $entity->save();
Chris@0 144 $this->fail('Exception did not fail');
Chris@0 145 }
Chris@0 146 catch (EntityStorageException $exception) {
Chris@0 147 $this->assertInstanceOf(\PHPUnit_Framework_Error_Warning::class, $exception->getPrevious());
Chris@0 148 $this->assertEquals($exception->getMessage(), 'Missing file with ID 9999.');
Chris@0 149 $this->assertEmpty($entity->image_test->width);
Chris@0 150 $this->assertEmpty($entity->image_test->height);
Chris@0 151 }
Chris@0 152
Chris@0 153 }
Chris@0 154
Chris@0 155 }