ImageFieldDisplayTest.php 13.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php

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

namespace Drupal\image\Tests;

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

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

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
  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));
52

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

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

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

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

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

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

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

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

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

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

  /**
   * 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,
160
      'description' => '[site:name]_description',
161
162
163
164
    );
    $widget_settings = array(
      'preview_image_style' => 'medium',
    );
165
    $instance = $this->createImageField($field_name, 'article', array(), $instance_settings, $widget_settings);
166
167

    $this->drupalGet('node/add/article');
168
169
    $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.');
170
    $this->assertText(t('Images must be between 10x10 and 100x100 pixels.'), 'Image widget allowed resolution displayed on article form.');
171
172
173
174
175

    // 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');
176
177
    $this->assertFieldByName($field_name . '[0][alt]', '', 'Alt field displayed on article form.');
    $this->assertFieldByName($field_name . '[0][title]', '', 'Title field displayed on article form.');
178
179
    // Verify that the attached image is being previewed using the 'medium'
    // style.
180
    $node = node_load($nid, TRUE);
181
182
    $image_style = array(
      '#theme' => 'image_style',
183
      '#uri' => file_load($node->{$field_name}->target_id)->getFileUri(),
184
185
186
      '#width' => 220,
      '#height' => 110,
      '#style_name' => 'medium',
187
    );
188
    $default_output = drupal_render($image_style);
189
    $this->assertRaw($default_output, "Preview image is displayed using 'medium' style.");
190
191

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

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

  /**
   * 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'));
237
    $this->drupalGet('node/' . $node->id());
238
239
    // Verify that no image is displayed on the page by checking for the class
    // that would be used on the image field.
240
    $this->assertNoPattern('<div class="(.*?)field-name-' . strtr($field_name, '_', '-') . '(.*?)">', 'No image displayed when no image is attached and no default image specified.');
241
242
243

    // Add a default image to the public imagefield instance.
    $images = $this->drupalGetTestFiles('image');
244
245
    $alt = $this->randomString(512);
    $title = $this->randomString(1024);
246
    $edit = array(
247
248
249
      'files[field_settings_default_image_fid]' => drupal_realpath($images[0]->uri),
      'field[settings][default_image][alt]' => $alt,
      'field[settings][default_image][title]' => $title,
250
    );
251
    $this->drupalPostForm("admin/structure/types/manage/article/fields/node.article.$field_name/field", $edit, t('Save field settings'));
252
253
    // Clear field info cache so the new default image is detected.
    field_info_cache_clear();
254
    $field = field_info_field('node', $field_name);
255
256
    $default_image = $field->getFieldSetting('default_image');
    $file = file_load($default_image['fid']);
257
258
259
260
    $this->assertTrue($file->isPermanent(), 'The default image status is permanent.');
    $image = array(
      '#theme' => 'image',
      '#uri' => $file->getFileUri(),
261
262
263
264
      '#alt' => $alt,
      '#title' => $title,
      '#width' => 40,
      '#height' => 20,
265
266
    );
    $default_output = drupal_render($image);
267
    $this->drupalGet('node/' . $node->id());
268
    $this->assertRaw($default_output, 'Default image displayed when no user supplied image is present.');
269
270
271
272

    // Create a node with an image attached and ensure that the default image
    // is not displayed.
    $nid = $this->uploadNodeImage($images[1], $field_name, 'article');
273
    $node = node_load($nid, TRUE);
274
275
    $image = array(
      '#theme' => 'image',
276
      '#uri' => file_load($node->{$field_name}->target_id)->getFileUri(),
277
278
      '#width' => 40,
      '#height' => 20,
279
    );
280
    $image_output = drupal_render($image);
281
    $this->drupalGet('node/' . $nid);
282
283
    $this->assertNoRaw($default_output, 'Default image is not displayed when user supplied image is present.');
    $this->assertRaw($image_output, 'User supplied image is displayed.');
284
285
286

    // Remove default image from the field and make sure it is no longer used.
    $edit = array(
287
      'field[settings][default_image][fid][fids]' => 0,
288
    );
289
    $this->drupalPostForm("admin/structure/types/manage/article/fields/node.article.$field_name/field", $edit, t('Save field settings'));
290
291
    // Clear field info cache so the new default image is detected.
    field_info_cache_clear();
292
    $field = field_info_field('node', $field_name);
293
294
    $default_image = $field->getFieldSetting('default_image');
    $this->assertFalse($default_image['fid'], 'Default image removed from field.');
295
296
297
298
299
300
    // 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(
301
302
303
      'files[field_settings_default_image_fid]' => drupal_realpath($images[1]->uri),
      'field[settings][default_image][alt]' => $alt,
      'field[settings][default_image][title]' => $title,
304
    );
305
    $this->drupalPostForm('admin/structure/types/manage/article/fields/node.article.' . $private_field_name . '/field', $edit, t('Save field settings'));
306
307
308
    // Clear field info cache so the new default image is detected.
    field_info_cache_clear();

309
    $private_field = field_info_field('node', $private_field_name);
310
311
    $default_image = $private_field->getFieldSetting('default_image');
    $file = file_load($default_image['fid']);
312
313
    $this->assertEqual('private', file_uri_scheme($file->getFileUri()), 'Default image uses private:// scheme.');
    $this->assertTrue($file->isPermanent(), 'The default image status is permanent.');
314
315
316
    // Create a new node with no image attached and ensure that default private
    // image is displayed.
    $node = $this->drupalCreateNode(array('type' => 'article'));
317
318
319
    $image = array(
      '#theme' => 'image',
      '#uri' => $file->getFileUri(),
320
321
322
323
      '#alt' => $alt,
      '#title' => $title,
      '#width' => 40,
      '#height' => 20,
324
325
    );
    $default_output = drupal_render($image);
326
    $this->drupalGet('node/' . $node->id());
327
    $this->assertRaw($default_output, 'Default private image displayed when no user supplied image is present.');
328
329
  }
}