ImageItemTest.php 5.88 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\image\Kernel;
4

5
use Drupal\Core\Entity\EntityStorageException;
6
use Drupal\Core\Field\FieldItemInterface;
7 8
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
9
use Drupal\entity_test\Entity\EntityTest;
10
use Drupal\field\Entity\FieldConfig;
11
use Drupal\Tests\field\Kernel\FieldKernelTestBase;
12
use Drupal\field\Entity\FieldStorageConfig;
13
use Drupal\file\Entity\File;
14
use Drupal\user\Entity\Role;
15
use PHPUnit\Framework\Error\Warning;
16 17

/**
18 19 20
 * Tests using entity fields of the image field type.
 *
 * @group image
21
 */
22
class ImageItemTest extends FieldKernelTestBase {
23 24 25 26 27 28

  /**
   * Modules to enable.
   *
   * @var array
   */
29
  protected static $modules = ['file', 'image'];
30 31 32 33

  /**
   * Created file entity.
   *
34
   * @var \Drupal\file\Entity\File
35 36 37
   */
  protected $image;

38 39 40 41 42
  /**
   * @var \Drupal\Core\Image\ImageFactory
   */
  protected $imageFactory;

43
  protected function setUp(): void {
44 45
    parent::setUp();

46 47 48 49 50 51 52 53
    $this->installEntitySchema('user');
    $this->installConfig(['user']);
    // Give anonymous users permission to access content, so that we can view
    // and download public file.
    $anonymous_role = Role::load(Role::ANONYMOUS_ID);
    $anonymous_role->grantPermission('access content');
    $anonymous_role->save();

54
    $this->installEntitySchema('file');
55
    $this->installSchema('file', ['file_usage']);
56

57
    FieldStorageConfig::create([
58
      'entity_type' => 'entity_test',
59
      'field_name' => 'image_test',
60
      'type' => 'image',
61
      'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
62
    ])->save();
63
    FieldConfig::create([
64 65 66
      'entity_type' => 'entity_test',
      'field_name' => 'image_test',
      'bundle' => 'entity_test',
67 68 69
      'settings' => [
        'file_extensions' => 'jpg',
      ],
70
    ])->save();
71
    \Drupal::service('file_system')->copy($this->root . '/core/misc/druplicon.png', 'public://example.jpg');
72
    $this->image = File::create([
73
      'uri' => 'public://example.jpg',
74
    ]);
75
    $this->image->save();
76
    $this->imageFactory = $this->container->get('image.factory');
77 78 79 80 81 82 83
  }

  /**
   * Tests using entity fields of the image field type.
   */
  public function testImageItem() {
    // Create a test entity with the image field set.
84
    $entity = EntityTest::create();
85
    $entity->image_test->target_id = $this->image->id();
86 87 88
    $entity->image_test->alt = $alt = $this->randomMachineName();
    $entity->image_test->title = $title = $this->randomMachineName();
    $entity->name->value = $this->randomMachineName();
89 90
    $entity->save();

91
    $entity = EntityTest::load($entity->id());
92 93
    $this->assertInstanceOf(FieldItemListInterface::class, $entity->image_test);
    $this->assertInstanceOf(FieldItemInterface::class, $entity->image_test[0]);
94
    $this->assertEqual($entity->image_test->target_id, $this->image->id());
95 96
    $this->assertEqual($entity->image_test->alt, $alt);
    $this->assertEqual($entity->image_test->title, $title);
97 98 99
    $image = $this->imageFactory->get('public://example.jpg');
    $this->assertEqual($entity->image_test->width, $image->getWidth());
    $this->assertEqual($entity->image_test->height, $image->getHeight());
100 101 102 103
    $this->assertEqual($entity->image_test->entity->id(), $this->image->id());
    $this->assertEqual($entity->image_test->entity->uuid(), $this->image->uuid());

    // Make sure the computed entity reflects updates to the referenced file.
104
    \Drupal::service('file_system')->copy($this->root . '/core/misc/druplicon.png', 'public://example-2.jpg');
105
    $image2 = File::create([
106
      'uri' => 'public://example-2.jpg',
107
    ]);
108 109
    $image2->save();

110
    $entity->image_test->target_id = $image2->id();
111
    $entity->image_test->alt = $new_alt = $this->randomMachineName();
112 113 114 115
    // The width and height is only updated when width is not set.
    $entity->image_test->width = NULL;
    $entity->save();
    $this->assertEqual($entity->image_test->entity->id(), $image2->id());
116
    $this->assertEqual($entity->image_test->entity->getFileUri(), $image2->getFileUri());
117 118 119
    $image = $this->imageFactory->get('public://example-2.jpg');
    $this->assertEqual($entity->image_test->width, $image->getWidth());
    $this->assertEqual($entity->image_test->height, $image->getHeight());
120 121 122 123
    $this->assertEqual($entity->image_test->alt, $new_alt);

    // Check that the image item can be set to the referenced file directly.
    $entity->image_test = $this->image;
124
    $this->assertEqual($entity->image_test->target_id, $this->image->id());
125 126 127

    // Delete the image and try to save the entity again.
    $this->image->delete();
128
    $entity = EntityTest::create(['mame' => $this->randomMachineName()]);
129
    $entity->save();
130

131
    // Test image item properties.
132
    $expected = ['target_id', 'entity', 'alt', 'title', 'width', 'height'];
133 134 135
    $properties = $entity->getFieldDefinition('image_test')->getFieldStorageDefinition()->getPropertyDefinitions();
    $this->assertEqual(array_keys($properties), $expected);

136
    // Test the generateSampleValue() method.
137
    $entity = EntityTest::create();
138 139
    $entity->image_test->generateSampleItems();
    $this->entityValidateAndSave($entity);
140
    $this->assertEqual($entity->image_test->entity->get('filemime')->value, 'image/jpeg');
141 142
  }

143 144 145 146 147 148 149 150 151 152 153 154 155 156
  /**
   * Tests a malformed image.
   */
  public function testImageItemMalformed() {
    // Validate entity is an image and don't gather dimensions if it is not.
    $entity = EntityTest::create();
    $entity->image_test = NULL;
    $entity->image_test->target_id = 9999;
    // PHPUnit re-throws E_USER_WARNING as an exception.
    try {
      $entity->save();
      $this->fail('Exception did not fail');
    }
    catch (EntityStorageException $exception) {
157
      $this->assertInstanceOf(Warning::class, $exception->getPrevious());
158 159 160 161 162 163 164
      $this->assertEquals($exception->getMessage(), 'Missing file with ID 9999.');
      $this->assertEmpty($entity->image_test->width);
      $this->assertEmpty($entity->image_test->height);
    }

  }

165
}