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

3
namespace Drupal\Tests\image\Kernel;
4

5
use Drupal\Core\Field\FieldItemInterface;
6
7
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
8
use Drupal\entity_test\Entity\EntityTest;
9
use Drupal\field\Entity\FieldConfig;
10
use Drupal\Tests\field\Kernel\FieldKernelTestBase;
11
use Drupal\field\Entity\FieldStorageConfig;
12
use Drupal\file\Entity\File;
13
14

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

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('file', 'image');

  /**
   * Created file entity.
   *
31
   * @var \Drupal\file\Entity\File
32
33
34
   */
  protected $image;

35
36
37
38
39
  /**
   * @var \Drupal\Core\Image\ImageFactory
   */
  protected $imageFactory;

40
  protected function setUp() {
41
42
    parent::setUp();

43
44
    $this->installEntitySchema('file');
    $this->installSchema('file', array('file_usage'));
45

46
    FieldStorageConfig::create(array(
47
      'entity_type' => 'entity_test',
48
      'field_name' => 'image_test',
49
      'type' => 'image',
50
      'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
51
    ))->save();
52
    FieldConfig::create([
53
54
55
      'entity_type' => 'entity_test',
      'field_name' => 'image_test',
      'bundle' => 'entity_test',
56
57
58
      'settings' => [
        'file_extensions' => 'jpg',
      ],
59
    ])->save();
60
    file_unmanaged_copy(\Drupal::root() . '/core/misc/druplicon.png', 'public://example.jpg');
61
    $this->image = File::create([
62
      'uri' => 'public://example.jpg',
63
    ]);
64
    $this->image->save();
65
    $this->imageFactory = $this->container->get('image.factory');
66
67
68
69
70
71
72
  }

  /**
   * Tests using entity fields of the image field type.
   */
  public function testImageItem() {
    // Create a test entity with the image field set.
73
    $entity = EntityTest::create();
74
    $entity->image_test->target_id = $this->image->id();
75
76
77
    $entity->image_test->alt = $alt = $this->randomMachineName();
    $entity->image_test->title = $title = $this->randomMachineName();
    $entity->name->value = $this->randomMachineName();
78
79
    $entity->save();

80
    $entity = EntityTest::load($entity->id());
81
    $this->assertTrue($entity->image_test instanceof FieldItemListInterface, 'Field implements interface.');
82
    $this->assertTrue($entity->image_test[0] instanceof FieldItemInterface, 'Field item implements interface.');
83
    $this->assertEqual($entity->image_test->target_id, $this->image->id());
84
85
    $this->assertEqual($entity->image_test->alt, $alt);
    $this->assertEqual($entity->image_test->title, $title);
86
87
88
    $image = $this->imageFactory->get('public://example.jpg');
    $this->assertEqual($entity->image_test->width, $image->getWidth());
    $this->assertEqual($entity->image_test->height, $image->getHeight());
89
90
91
92
    $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.
93
    file_unmanaged_copy(\Drupal::root() . '/core/misc/druplicon.png', 'public://example-2.jpg');
94
    $image2 = File::create([
95
      'uri' => 'public://example-2.jpg',
96
    ]);
97
98
    $image2->save();

99
    $entity->image_test->target_id = $image2->id();
100
    $entity->image_test->alt = $new_alt = $this->randomMachineName();
101
102
103
104
    // 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());
105
    $this->assertEqual($entity->image_test->entity->getFileUri(), $image2->getFileUri());
106
107
108
    $image = $this->imageFactory->get('public://example-2.jpg');
    $this->assertEqual($entity->image_test->width, $image->getWidth());
    $this->assertEqual($entity->image_test->height, $image->getHeight());
109
110
111
112
    $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;
113
    $this->assertEqual($entity->image_test->target_id, $this->image->id());
114
115
116

    // Delete the image and try to save the entity again.
    $this->image->delete();
117
    $entity = EntityTest::create(array('mame' => $this->randomMachineName()));
118
    $entity->save();
119

120
121
122
123
124
    // Test image item properties.
    $expected = array('target_id', 'entity', 'alt', 'title', 'width', 'height');
    $properties = $entity->getFieldDefinition('image_test')->getFieldStorageDefinition()->getPropertyDefinitions();
    $this->assertEqual(array_keys($properties), $expected);

125
    // Test the generateSampleValue() method.
126
    $entity = EntityTest::create();
127
128
    $entity->image_test->generateSampleItems();
    $this->entityValidateAndSave($entity);
129
    $this->assertEqual($entity->image_test->entity->get('filemime')->value, 'image/jpeg');
130
131
132
  }

}