annotate core/modules/image/tests/src/Kernel/ImageItemTest.php @ 19:fa3358dc1485 tip

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