summaryrefslogtreecommitdiffstats
path: root/src/Tests/TokenFieldUiTest.php
blob: f7983e73b59b2df17209ee7050ca9c57f28891ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php

namespace Drupal\token\Tests;

use Drupal\node\Entity\NodeType;
use Drupal\node\Entity\Node;
use Drupal\file\Entity\File;
use Drupal\image\Entity\ImageStyle;

/**
 * Tests field ui.
 *
 * @group token
 */
class TokenFieldUiTest extends TokenTestBase {

  /**
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $adminUser;

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = ['field_ui', 'node', 'image'];

  /**
   * {@inheritdoc}
   */
  public function setUp($modules = []) {
    parent::setUp();
    $this->adminUser = $this->drupalCreateUser(['administer content types', 'administer node fields']);
    $this->drupalLogin($this->adminUser);

    $node_type = NodeType::create([
      'type' => 'article',
      'name' => 'Article',
      'description' => "Use <em>articles</em> for time-sensitive content like news, press releases or blog posts.",
    ]);
    $node_type->save();

    entity_create('field_storage_config', array(
      'field_name' => 'field_body',
      'entity_type' => 'node',
      'type' => 'text_with_summary',
    ))->save();
    entity_create('field_config', array(
      'field_name' => 'field_body',
      'label' => 'Body',
      'entity_type' => 'node',
      'bundle' => 'article',
    ))->save();
    entity_create('field_storage_config', array(
      'field_name' => 'field_image',
      'entity_type' => 'node',
      'type' => 'image',
    ))->save();
    entity_create('field_config', array(
      'field_name' => 'field_image',
      'label' => 'Image',
      'entity_type' => 'node',
      'bundle' => 'article',
    ))->save();
    entity_create('field_storage_config', array(
      'field_name' => 'field_image_2',
      'entity_type' => 'node',
      'type' => 'image',
    ))->save();
    entity_create('field_config', array(
      'field_name' => 'field_image_2',
      'label' => 'Image 2',
      'entity_type' => 'node',
      'bundle' => 'article',
    ))->save();
    entity_create('field_storage_config', array(
      'field_name' => 'multivalued_field_image',
      'entity_type' => 'node',
      'type' => 'image',
    ))->save();
    entity_create('field_config', array(
      'field_name' => 'multivalued_field_image',
      'label' => 'Multivalued field image',
      'entity_type' => 'node',
      'bundle' => 'article',
    ))->save();

    entity_get_form_display('node', 'article', 'default')
      ->setComponent('field_body', [
        'type' => 'text_textarea_with_summary',
        'settings' => [
          'rows' => '9',
          'summary_rows' => '3',
        ],
        'weight' => 5,
      ])
      ->save();
  }

  public function testFileFieldUi() {
    $this->drupalGet('admin/structure/types/manage/article/fields/node.article.field_image');

    // Ensure the 'Browse available tokens' link is present and correct.
    $this->assertLink('Browse available tokens.');
    $this->assertLinkByHref('token/tree');

    // Ensure that the default file directory value validates correctly.
    $this->drupalPostForm(NULL, [], t('Save settings'));
    $this->assertText(t('Saved Image configuration.'));
  }

  public function testFieldDescriptionTokens() {
    $edit = [
      'description' => 'The site is called [site:name].',
    ];
    $this->drupalPostForm('admin/structure/types/manage/article/fields/node.article.field_body', $edit, 'Save settings');

    $this->drupalGet('node/add/article');
    $this->assertText('The site is called Drupal.');
  }

  /**
   * Test that tokens are correctly provided and replaced for the image fields.
   */
  public function testImageFieldTokens() {
    // Generate 2 different test images.
    file_unmanaged_copy(\Drupal::root() . '/core/misc/druplicon.png', 'public://example1.png');
    file_unmanaged_copy(\Drupal::root() . '/core/misc/loading.gif', 'public://example2.gif');

    // Resize the test images so that they will be scaled down during token
    // replacement.
    $image1 = \Drupal::service('image.factory')->get('public://example1.png');
    $image1->resize(500, 500);
    $image1->save();
    $image2 = \Drupal::service('image.factory')->get('public://example2.gif');
    $image2->resize(500, 500);
    $image2->save();

    /** @var \Drupal\file\Entity\File $image1 */
    $image1 = File::create(['uri' => 'public://example1.png']);
    $image1->save();
    /** @var \Drupal\file\Entity\File $image2 */
    $image2 = File::create(['uri' => 'public://example2.gif']);
    $image2->save();

    $node = Node::create([
      'title' => 'Test node title',
      'type' => 'article',
      'field_image' => [
        [
          'target_id' => $image1->id(),
        ],
      ],
      'field_image_2' => [
        [
          'target_id' => $image2->id(),
        ],
      ],
      'multivalued_field_image' => [
        ['target_id' => $image1->id()],
        ['target_id' => $image2->id()],
      ],
    ]);
    $node->save();

    // Obtain the file size and dimension of the images that will be scaled
    // down during token replacement by applying the styles here.
    $style_thumbnail = ImageStyle::load('thumbnail');
    $style_thumbnail->createDerivative('public://example1.png', 'public://styles/thumbnail/public/example1-test.png');
    $style_thumbnail->createDerivative('public://example2.gif', 'public://styles/thumbnail/public/example2-test.gif');
    $image_1_thumbnail = \Drupal::service('image.factory')->get('public://styles/thumbnail/public/example1-test.png');
    $image_2_thumbnail = \Drupal::service('image.factory')->get('public://styles/thumbnail/public/example2-test.gif');
    $style_medium = ImageStyle::load('medium');
    $style_medium->createDerivative('public://example1.png', 'public://styles/medium/public/example1-test.png');
    $style_medium->createDerivative('public://example2.gif', 'public://styles/medium/public/example2-test.gif');
    $image_1_medium = \Drupal::service('image.factory')->get('public://styles/medium/public/example1-test.png');
    $image_2_medium = \Drupal::service('image.factory')->get('public://styles/medium/public/example2-test.gif');
    $style_large = ImageStyle::load('large');
    $style_large->createDerivative('public://example1.png', 'public://styles/large/public/example1-test.png');
    $style_large->createDerivative('public://example2.gif', 'public://styles/large/public/example2-test.gif');
    $image_1_large = \Drupal::service('image.factory')->get('public://styles/large/public/example1-test.png');
    $image_2_large = \Drupal::service('image.factory')->get('public://styles/large/public/example2-test.gif');

    // Delete the image derivatives, to make sure they are re-created.
    unlink('public://styles/thumbnail/public/example1-test.png');
    unlink('public://styles/medium/public/example1-test.png');
    unlink('public://styles/large/public/example1-test.png');
    unlink('public://styles/thumbnail/public/example2-test.gif');
    unlink('public://styles/medium/public/example2-test.gif');
    unlink('public://styles/large/public/example2-test.gif');

    $tokens = [
      // field_image
      'field_image:thumbnail:mimetype' => 'image/png',
      'field_image:medium:mimetype' => 'image/png',
      'field_image:large:mimetype' => 'image/png',
      'field_image:thumbnail:filesize' => $image_1_thumbnail->getFileSize(),
      'field_image:medium:filesize' => $image_1_medium->getFileSize(),
      'field_image:large:filesize' => $image_1_large->getFileSize(),
      'field_image:thumbnail:height' => '100',
      'field_image:medium:height' => '220',
      'field_image:large:height' => '480',
      'field_image:thumbnail:width' => '100',
      'field_image:medium:width' => '220',
      'field_image:large:width' => '480',
      'field_image:thumbnail:uri' => 'public://styles/thumbnail/public/example1.png',
      'field_image:medium:uri' => 'public://styles/medium/public/example1.png',
      'field_image:large:uri' => 'public://styles/large/public/example1.png',
      'field_image:thumbnail:url' => $style_thumbnail->buildUrl('public://example1.png'),
      'field_image:medium:url' => $style_medium->buildUrl('public://example1.png'),
      'field_image:large:url' => $style_large->buildUrl('public://example1.png'),
      'field_image:thumbnail' => $style_thumbnail->buildUrl('public://example1.png'),
      'field_image:medium' => $style_medium->buildUrl('public://example1.png'),
      'field_image:large' => $style_large->buildUrl('public://example1.png'),
      // field_image_2
      'field_image_2:thumbnail:mimetype' => 'image/gif',
      'field_image_2:medium:mimetype' => 'image/gif',
      'field_image_2:large:mimetype' => 'image/gif',
      'field_image_2:thumbnail:filesize' => $image_2_thumbnail->getFileSize(),
      'field_image_2:medium:filesize' => $image_2_medium->getFileSize(),
      'field_image_2:large:filesize' => $image_2_large->getFileSize(),
      'field_image_2:thumbnail:height' => '100',
      'field_image_2:medium:height' => '220',
      'field_image_2:large:height' => '480',
      'field_image_2:thumbnail:width' => '100',
      'field_image_2:medium:width' => '220',
      'field_image_2:large:width' => '480',
      'field_image_2:thumbnail:uri' => 'public://styles/thumbnail/public/example2.gif',
      'field_image_2:medium:uri' => 'public://styles/medium/public/example2.gif',
      'field_image_2:large:uri' => 'public://styles/large/public/example2.gif',
      'field_image_2:thumbnail:url' => $style_thumbnail->buildUrl('public://example2.gif'),
      'field_image_2:medium:url' => $style_medium->buildUrl('public://example2.gif'),
      'field_image_2:large:url' => $style_large->buildUrl('public://example2.gif'),
      'field_image_2:thumbnail' => $style_thumbnail->buildUrl('public://example2.gif'),
      'field_image_2:medium' => $style_medium->buildUrl('public://example2.gif'),
      'field_image_2:large' => $style_large->buildUrl('public://example2.gif'),
      // multivalued_field_image:0, test for thumbnail image style only.
      'multivalued_field_image:0:thumbnail:mimetype' => 'image/png',
      'multivalued_field_image:0:thumbnail:filesize' => $image_1_thumbnail->getFileSize(),
      'multivalued_field_image:0:thumbnail:height' => '100',
      'multivalued_field_image:0:thumbnail:width' => '100',
      'multivalued_field_image:0:thumbnail:uri' => 'public://styles/thumbnail/public/example1.png',
      'multivalued_field_image:0:thumbnail:url' => $style_thumbnail->buildUrl('public://example1.png'),
      'multivalued_field_image:0:thumbnail' => $style_thumbnail->buildUrl('public://example1.png'),
      // multivalued_field_image:1, test for medium image style only.
      'multivalued_field_image:1:medium:mimetype' => 'image/gif',
      'multivalued_field_image:1:medium:filesize' => $image_2_medium->getFileSize(),
      'multivalued_field_image:1:medium:height' => '220',
      'multivalued_field_image:1:medium:width' => '220',
      'multivalued_field_image:1:medium:uri' => 'public://styles/medium/public/example2.gif',
      'multivalued_field_image:1:medium:url' => $style_medium->buildUrl('public://example2.gif'),
      'multivalued_field_image:1:medium' => $style_medium->buildUrl('public://example2.gif'),
    ];
    $this->assertTokens('node', ['node' => $node], $tokens);

    /** @var \Drupal\token\Token $token_service */
    $token_service = \Drupal::service('token');

    // Test one of the image style's token info for cardinality 1 image field.
    $token_info = $token_service->getTokenInfo('node-field_image', 'thumbnail');
    $this->assertEqual('Thumbnail (100×100)', $token_info['name']);
    $this->assertEqual('Represents the image in the given image style.', $token_info['description']);

    // Test one of the image style's token info for a multivalued image field.
    $token_info = $token_service->getTokenInfo('node-multivalued_field_image', 'medium');
    $this->assertEqual('Medium (220×220)', $token_info['name']);
    $this->assertEqual('Represents the image in the given image style.', $token_info['description']);

    // Test few of the image styles' properties token info.
    $token_info = $token_service->getTokenInfo('image_with_image_style', 'mimetype');
    $this->assertEqual('MIME type', $token_info['name']);
    $this->assertEqual('The MIME type (image/png, image/bmp, etc.) of the image.', $token_info['description']);

    $token_info = $token_service->getTokenInfo('image_with_image_style', 'filesize');
    $this->assertEqual('File size', $token_info['name']);
    $this->assertEqual('The file size of the image.', $token_info['description']);
  }

}