ImageFieldDisplayTest.php 14.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
<?php

/**
 * @file
 * Definition of Drupal\image\Tests\ImageFieldDisplayTest.
 */

namespace Drupal\image\Tests;

10
11
use Drupal\Core\Field\FieldDefinitionInterface;

12
13
14
15
/**
 * Test class to check that formatters and display settings are working.
 */
class ImageFieldDisplayTest extends ImageFieldTestBase {
16
17
18
19
20
21
22
23

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

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
  public static function getInfo() {
    return array(
      'name' => 'Image field display tests',
      'description' => 'Test the display of image fields.',
      'group' => 'Image',
    );
  }

  /**
   * Test image formatters on node display for public files.
   */
  function testImageFieldFormattersPublic() {
    $this->_testImageFieldFormatters('public');
  }

  /**
   * Test image formatters on node display for private files.
   */
  function testImageFieldFormattersPrivate() {
    // Remove access content permission from anonymous users.
    user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array('access content' => FALSE));
    $this->_testImageFieldFormatters('private');
  }

  /**
   * Test image formatters on node display.
   */
  function _testImageFieldFormatters($scheme) {
    $field_name = strtolower($this->randomName());
    $this->createImageField($field_name, 'article', array('uri_scheme' => $scheme));
54

55
56
57
    // Create a new node with an image attached.
    $test_image = current($this->drupalGetTestFiles('image'));
    $nid = $this->uploadNodeImage($test_image, $field_name, 'article');
58
    $node = node_load($nid, TRUE);
59
60

    // Test that the default formatter is being used.
61
    $image_uri = file_load($node->{$field_name}->target_id)->getFileUri();
62
63
64
65
66
    $image = array(
      '#theme' => 'image',
      '#uri' => $image_uri,
      '#width' => 40,
      '#height' => 20,
67
    );
68
    $default_output = drupal_render($image);
69
    $this->assertRaw($default_output, 'Default formatter displaying correctly on full node view.');
70
71

    // Test the image linked to file formatter.
72
73
74
75
    $display_options = array(
      'type' => 'image',
      'settings' => array('image_link' => 'file'),
    );
76
    $display = entity_get_display('node', $node->getType(), 'default');
77
78
79
    $display->setComponent($field_name, $display_options)
      ->save();

80
81
82
83
84
85
86
    $image = array(
      '#theme' => 'image',
      '#uri' => $image_uri,
      '#width' => 40,
      '#height' => 20,
    );
    $default_output = l($image, file_create_url($image_uri), array('html' => TRUE));
87
    $this->drupalGet('node/' . $nid);
88
    $this->assertRaw($default_output, 'Image linked to file formatter displaying correctly on full node view.');
89
    // Verify that the image can be downloaded.
90
    $this->assertEqual(file_get_contents($test_image->uri), $this->drupalGet(file_create_url($image_uri)), 'File was downloaded successfully.');
91
92
93
    if ($scheme == 'private') {
      // Only verify HTTP headers when using private scheme and the headers are
      // sent by Drupal.
94
95
      $this->assertEqual($this->drupalGetHeader('Content-Type'), 'image/png', 'Content-Type header was sent.');
      $this->assertTrue(strstr($this->drupalGetHeader('Cache-Control'),'private') !== FALSE, 'Cache-Control header was sent.');
96
97
98
99

      // Log out and try to access the file.
      $this->drupalLogout();
      $this->drupalGet(file_create_url($image_uri));
100
      $this->assertResponse('403', 'Access denied to original image as anonymous user.');
101
102
103
104
105
106

      // Log in again.
      $this->drupalLogin($this->admin_user);
    }

    // Test the image linked to content formatter.
107
108
109
    $display_options['settings']['image_link'] = 'content';
    $display->setComponent($field_name, $display_options)
      ->save();
110
111
112
113
114
115
116
    $image = array(
      '#theme' => 'image',
      '#uri' => $image_uri,
      '#width' => 40,
      '#height' => 20,
    );
    $default_output = l($image, 'node/' . $nid, array('html' => TRUE, 'attributes' => array('class' => 'active')));
117
    $this->drupalGet('node/' . $nid);
118
    $this->assertRaw($default_output, 'Image linked to content formatter displaying correctly on full node view.');
119
120

    // Test the image style 'thumbnail' formatter.
121
122
123
124
125
    $display_options['settings']['image_link'] = '';
    $display_options['settings']['image_style'] = 'thumbnail';
    $display->setComponent($field_name, $display_options)
      ->save();

126
127
    // Ensure the derivative image is generated so we do not have to deal with
    // image style callback paths.
128
    $this->drupalGet(entity_load('image_style', 'thumbnail')->buildUrl($image_uri));
129
130
131
132
133
134
135
136
    $image_style = array(
      '#theme' => 'image_style',
      '#uri' => $image_uri,
      '#width' => 100,
      '#height' => 50,
      '#style_name' => 'thumbnail',
    );
    $default_output = drupal_render($image_style);
137
    $this->drupalGet('node/' . $nid);
138
    $this->assertRaw($default_output, 'Image style thumbnail formatter displaying correctly on full node view.');
139
140
141
142

    if ($scheme == 'private') {
      // Log out and try to access the file.
      $this->drupalLogout();
143
      $this->drupalGet(entity_load('image_style', 'thumbnail')->buildUrl($image_uri));
144
      $this->assertResponse('403', 'Access denied to image style thumbnail as anonymous user.');
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
    }
  }

  /**
   * Tests for image field settings.
   */
  function testImageFieldSettings() {
    $test_image = current($this->drupalGetTestFiles('image'));
    list(, $test_image_extension) = explode('.', $test_image->filename);
    $field_name = strtolower($this->randomName());
    $instance_settings = array(
      'alt_field' => 1,
      'file_extensions' => $test_image_extension,
      'max_filesize' => '50 KB',
      'max_resolution' => '100x100',
      'min_resolution' => '10x10',
      'title_field' => 1,
162
      'description' => '[site:name]_description',
163
164
165
166
    );
    $widget_settings = array(
      'preview_image_style' => 'medium',
    );
167
    $instance = $this->createImageField($field_name, 'article', array(), $instance_settings, $widget_settings);
168
169

    $this->drupalGet('node/add/article');
170
171
    $this->assertText(t('50 KB limit.'), 'Image widget max file size is displayed on article form.');
    $this->assertText(t('Allowed types: ' . $test_image_extension . '.'), 'Image widget allowed file types displayed on article form.');
172
    $this->assertText(t('Images must be between 10x10 and 100x100 pixels.'), 'Image widget allowed resolution displayed on article form.');
173
174
175
176
177

    // We have to create the article first and then edit it because the alt
    // and title fields do not display until the image has been attached.
    $nid = $this->uploadNodeImage($test_image, $field_name, 'article');
    $this->drupalGet('node/' . $nid . '/edit');
178
179
    $this->assertFieldByName($field_name . '[0][alt]', '', 'Alt field displayed on article form.');
    $this->assertFieldByName($field_name . '[0][title]', '', 'Title field displayed on article form.');
180
181
    // Verify that the attached image is being previewed using the 'medium'
    // style.
182
    $node = node_load($nid, TRUE);
183
184
    $image_style = array(
      '#theme' => 'image_style',
185
      '#uri' => file_load($node->{$field_name}->target_id)->getFileUri(),
186
187
188
      '#width' => 220,
      '#height' => 110,
      '#style_name' => 'medium',
189
    );
190
    $default_output = drupal_render($image_style);
191
    $this->assertRaw($default_output, "Preview image is displayed using 'medium' style.");
192
193

    // Add alt/title fields to the image and verify that they are displayed.
194
195
    $image = array(
      '#theme' => 'image',
196
      '#uri' => file_load($node->{$field_name}->target_id)->getFileUri(),
197
198
199
200
      '#alt' => $this->randomName(),
      '#title' => $this->randomName(),
      '#width' => 40,
      '#height' => 20,
201
202
    );
    $edit = array(
203
204
      $field_name . '[0][alt]' => $image['#alt'],
      $field_name . '[0][title]' => $image['#title'],
205
    );
206
    $this->drupalPostForm('node/' . $nid . '/edit', $edit, t('Save and keep published'));
207
    $default_output = drupal_render($image);
208
    $this->assertRaw($default_output, 'Image displayed using user supplied alt and title attributes.');
209
210
211
212

    // Verify that alt/title longer than allowed results in a validation error.
    $test_size = 2000;
    $edit = array(
213
214
      $field_name . '[0][alt]' => $this->randomName($test_size),
      $field_name . '[0][title]' => $this->randomName($test_size),
215
    );
216
    $this->drupalPostForm('node/' . $nid . '/edit', $edit, t('Save and keep published'));
217
    $schema = $instance->getField()->getSchema();
218
    $this->assertRaw(t('Alternate text cannot be longer than %max characters but is currently %length characters long.', array(
219
      '%max' => $schema['columns']['alt']['length'],
220
221
222
      '%length' => $test_size,
    )));
    $this->assertRaw(t('Title cannot be longer than %max characters but is currently %length characters long.', array(
223
      '%max' => $schema['columns']['title']['length'],
224
225
      '%length' => $test_size,
    )));
226
227
228
229
230
231
232
233
234
235
236
237
238

    // Set cardinality to unlimited and add upload a second image.
    // The image widget is extending on the file widget, but the image field
    // type does not have the 'display_field' setting which is expected by
    // the file widget. This resulted in notices before when cardinality is not
    // 1, so we need to make sure the file widget prevents these notices by
    // providing all settings, even if they are not used.
    // @see FileWidget::formMultipleElements().
    $this->drupalPostForm('admin/structure/types/manage/article/fields/node.article.' . $field_name . '/field', array('field[cardinality]' => FieldDefinitionInterface::CARDINALITY_UNLIMITED), t('Save field settings'));
    $edit = array();
    $edit['files[' . $field_name . '_1][]'] = drupal_realpath($test_image->uri);
    $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
    $this->assertText(format_string('Article @title has been updated.', array('@title' => $node->getTitle())));
239
240
241
242
243
244
245
246
247
248
249
250
251
  }

  /**
   * Test use of a default image with an image field.
   */
  function testImageFieldDefaultImage() {
    // Create a new image field.
    $field_name = strtolower($this->randomName());
    $this->createImageField($field_name, 'article');

    // Create a new node, with no images and verify that no images are
    // displayed.
    $node = $this->drupalCreateNode(array('type' => 'article'));
252
    $this->drupalGet('node/' . $node->id());
253
254
    // Verify that no image is displayed on the page by checking for the class
    // that would be used on the image field.
255
    $this->assertNoPattern('<div class="(.*?)field-name-' . strtr($field_name, '_', '-') . '(.*?)">', 'No image displayed when no image is attached and no default image specified.');
256
257
258

    // Add a default image to the public imagefield instance.
    $images = $this->drupalGetTestFiles('image');
259
260
    $alt = $this->randomString(512);
    $title = $this->randomString(1024);
261
    $edit = array(
262
263
264
      'files[field_settings_default_image_fid]' => drupal_realpath($images[0]->uri),
      'field[settings][default_image][alt]' => $alt,
      'field[settings][default_image][title]' => $title,
265
    );
266
    $this->drupalPostForm("admin/structure/types/manage/article/fields/node.article.$field_name/field", $edit, t('Save field settings'));
267
268
    // Clear field info cache so the new default image is detected.
    field_info_cache_clear();
269
    $field = field_info_field('node', $field_name);
270
    $default_image = $field->getSetting('default_image');
271
    $file = file_load($default_image['fid']);
272
273
274
275
    $this->assertTrue($file->isPermanent(), 'The default image status is permanent.');
    $image = array(
      '#theme' => 'image',
      '#uri' => $file->getFileUri(),
276
277
278
279
      '#alt' => $alt,
      '#title' => $title,
      '#width' => 40,
      '#height' => 20,
280
281
    );
    $default_output = drupal_render($image);
282
    $this->drupalGet('node/' . $node->id());
283
    $this->assertRaw($default_output, 'Default image displayed when no user supplied image is present.');
284
285
286
287

    // Create a node with an image attached and ensure that the default image
    // is not displayed.
    $nid = $this->uploadNodeImage($images[1], $field_name, 'article');
288
    $node = node_load($nid, TRUE);
289
290
    $image = array(
      '#theme' => 'image',
291
      '#uri' => file_load($node->{$field_name}->target_id)->getFileUri(),
292
293
      '#width' => 40,
      '#height' => 20,
294
    );
295
    $image_output = drupal_render($image);
296
    $this->drupalGet('node/' . $nid);
297
298
    $this->assertNoRaw($default_output, 'Default image is not displayed when user supplied image is present.');
    $this->assertRaw($image_output, 'User supplied image is displayed.');
299
300
301

    // Remove default image from the field and make sure it is no longer used.
    $edit = array(
302
      'field[settings][default_image][fid][fids]' => 0,
303
    );
304
    $this->drupalPostForm("admin/structure/types/manage/article/fields/node.article.$field_name/field", $edit, t('Save field settings'));
305
306
    // Clear field info cache so the new default image is detected.
    field_info_cache_clear();
307
    $field = field_info_field('node', $field_name);
308
    $default_image = $field->getSetting('default_image');
309
    $this->assertFalse($default_image['fid'], 'Default image removed from field.');
310
311
312
313
314
315
    // Create an image field that uses the private:// scheme and test that the
    // default image works as expected.
    $private_field_name = strtolower($this->randomName());
    $this->createImageField($private_field_name, 'article', array('uri_scheme' => 'private'));
    // Add a default image to the new field.
    $edit = array(
316
317
318
      'files[field_settings_default_image_fid]' => drupal_realpath($images[1]->uri),
      'field[settings][default_image][alt]' => $alt,
      'field[settings][default_image][title]' => $title,
319
    );
320
    $this->drupalPostForm('admin/structure/types/manage/article/fields/node.article.' . $private_field_name . '/field', $edit, t('Save field settings'));
321
322
323
    // Clear field info cache so the new default image is detected.
    field_info_cache_clear();

324
    $private_field = field_info_field('node', $private_field_name);
325
    $default_image = $private_field->getSetting('default_image');
326
    $file = file_load($default_image['fid']);
327
328
    $this->assertEqual('private', file_uri_scheme($file->getFileUri()), 'Default image uses private:// scheme.');
    $this->assertTrue($file->isPermanent(), 'The default image status is permanent.');
329
330
331
    // Create a new node with no image attached and ensure that default private
    // image is displayed.
    $node = $this->drupalCreateNode(array('type' => 'article'));
332
333
334
    $image = array(
      '#theme' => 'image',
      '#uri' => $file->getFileUri(),
335
336
337
338
      '#alt' => $alt,
      '#title' => $title,
      '#width' => 40,
      '#height' => 20,
339
340
    );
    $default_output = drupal_render($image);
341
    $this->drupalGet('node/' . $node->id());
342
    $this->assertRaw($default_output, 'Default private image displayed when no user supplied image is present.');
343
344
  }
}