Chris@0: _testImageFieldFormatters('public'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test image formatters on node display for private files. Chris@0: */ Chris@0: public function testImageFieldFormattersPrivate() { Chris@0: // Remove access content permission from anonymous users. Chris@0: user_role_change_permissions(RoleInterface::ANONYMOUS_ID, ['access content' => FALSE]); Chris@0: $this->_testImageFieldFormatters('private'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test image formatters on node display. Chris@0: */ Chris@0: public function _testImageFieldFormatters($scheme) { Chris@0: /** @var \Drupal\Core\Render\RendererInterface $renderer */ Chris@0: $renderer = $this->container->get('renderer'); Chris@0: $node_storage = $this->container->get('entity.manager')->getStorage('node'); Chris@0: $field_name = strtolower($this->randomMachineName()); Chris@0: $field_settings = ['alt_field_required' => 0]; Chris@0: $instance = $this->createImageField($field_name, 'article', ['uri_scheme' => $scheme], $field_settings); Chris@0: Chris@0: // Go to manage display page. Chris@0: $this->drupalGet("admin/structure/types/manage/article/display"); Chris@0: Chris@0: // Test for existence of link to image styles configuration. Chris@0: $this->drupalPostAjaxForm(NULL, [], "{$field_name}_settings_edit"); Chris@0: $this->assertLinkByHref(\Drupal::url('entity.image_style.collection'), 0, 'Link to image styles configuration is found'); Chris@0: Chris@0: // Remove 'administer image styles' permission from testing admin user. Chris@0: $admin_user_roles = $this->adminUser->getRoles(TRUE); Chris@0: user_role_change_permissions(reset($admin_user_roles), ['administer image styles' => FALSE]); Chris@0: Chris@0: // Go to manage display page again. Chris@0: $this->drupalGet("admin/structure/types/manage/article/display"); Chris@0: Chris@0: // Test for absence of link to image styles configuration. Chris@0: $this->drupalPostAjaxForm(NULL, [], "{$field_name}_settings_edit"); Chris@0: $this->assertNoLinkByHref(\Drupal::url('entity.image_style.collection'), 'Link to image styles configuration is absent when permissions are insufficient'); Chris@0: Chris@0: // Restore 'administer image styles' permission to testing admin user Chris@0: user_role_change_permissions(reset($admin_user_roles), ['administer image styles' => TRUE]); Chris@0: Chris@0: // Create a new node with an image attached. Chris@0: $test_image = current($this->drupalGetTestFiles('image')); Chris@0: Chris@0: // Ensure that preview works. Chris@0: $this->previewNodeImage($test_image, $field_name, 'article'); Chris@0: Chris@0: // After previewing, make the alt field required. It cannot be required Chris@0: // during preview because the form validation will fail. Chris@0: $instance->setSetting('alt_field_required', 1); Chris@0: $instance->save(); Chris@0: Chris@0: // Create alt text for the image. Chris@0: $alt = $this->randomMachineName(); Chris@0: Chris@0: // Save node. Chris@0: $nid = $this->uploadNodeImage($test_image, $field_name, 'article', $alt); Chris@0: $node_storage->resetCache([$nid]); Chris@0: $node = $node_storage->load($nid); Chris@0: Chris@0: // Test that the default formatter is being used. Chris@0: $file = $node->{$field_name}->entity; Chris@0: $image_uri = $file->getFileUri(); Chris@0: $image = [ Chris@0: '#theme' => 'image', Chris@0: '#uri' => $image_uri, Chris@0: '#width' => 40, Chris@0: '#height' => 20, Chris@0: '#alt' => $alt, Chris@0: ]; Chris@0: $default_output = str_replace("\n", NULL, $renderer->renderRoot($image)); Chris@0: $this->assertRaw($default_output, 'Default formatter displaying correctly on full node view.'); Chris@0: Chris@0: // Test the image linked to file formatter. Chris@0: $display_options = [ Chris@0: 'type' => 'image', Chris@0: 'settings' => ['image_link' => 'file'], Chris@0: ]; Chris@0: $display = entity_get_display('node', $node->getType(), 'default'); Chris@0: $display->setComponent($field_name, $display_options) Chris@0: ->save(); Chris@0: Chris@0: $image = [ Chris@0: '#theme' => 'image', Chris@0: '#uri' => $image_uri, Chris@0: '#width' => 40, Chris@0: '#height' => 20, Chris@0: '#alt' => $alt, Chris@0: ]; Chris@0: $default_output = '' . $renderer->renderRoot($image) . ''; Chris@0: $this->drupalGet('node/' . $nid); Chris@0: $this->assertCacheTag($file->getCacheTags()[0]); Chris@0: // @todo Remove in https://www.drupal.org/node/2646744. Chris@0: $this->assertCacheContext('url.site'); Chris@0: $cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags'); Chris@0: $this->assertTrue(!preg_match('/ image_style\:/', $cache_tags_header), 'No image style cache tag found.'); Chris@0: $this->assertRaw($default_output, 'Image linked to file formatter displaying correctly on full node view.'); Chris@0: // Verify that the image can be downloaded. Chris@0: $this->assertEqual(file_get_contents($test_image->uri), $this->drupalGet(file_create_url($image_uri)), 'File was downloaded successfully.'); Chris@0: if ($scheme == 'private') { Chris@0: // Only verify HTTP headers when using private scheme and the headers are Chris@0: // sent by Drupal. Chris@0: $this->assertEqual($this->drupalGetHeader('Content-Type'), 'image/png', 'Content-Type header was sent.'); Chris@0: $this->assertTrue(strstr($this->drupalGetHeader('Cache-Control'), 'private') !== FALSE, 'Cache-Control header was sent.'); Chris@0: Chris@0: // Log out and try to access the file. Chris@0: $this->drupalLogout(); Chris@0: $this->drupalGet(file_create_url($image_uri)); Chris@0: $this->assertResponse('403', 'Access denied to original image as anonymous user.'); Chris@0: Chris@0: // Log in again. Chris@0: $this->drupalLogin($this->adminUser); Chris@0: } Chris@0: Chris@0: // Test the image linked to content formatter. Chris@0: $display_options['settings']['image_link'] = 'content'; Chris@0: $display->setComponent($field_name, $display_options) Chris@0: ->save(); Chris@0: $image = [ Chris@0: '#theme' => 'image', Chris@0: '#uri' => $image_uri, Chris@0: '#width' => 40, Chris@0: '#height' => 20, Chris@0: ]; Chris@0: $this->drupalGet('node/' . $nid); Chris@0: $this->assertCacheTag($file->getCacheTags()[0]); Chris@0: $cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags'); Chris@0: $this->assertTrue(!preg_match('/ image_style\:/', $cache_tags_header), 'No image style cache tag found.'); Chris@0: $elements = $this->xpath( Chris@0: '//a[@href=:path]/img[@src=:url and @alt=:alt and @width=:width and @height=:height]', Chris@0: [ Chris@0: ':path' => $node->url(), Chris@0: ':url' => file_url_transform_relative(file_create_url($image['#uri'])), Chris@0: ':width' => $image['#width'], Chris@0: ':height' => $image['#height'], Chris@0: ':alt' => $alt, Chris@0: ] Chris@0: ); Chris@0: $this->assertEqual(count($elements), 1, 'Image linked to content formatter displaying correctly on full node view.'); Chris@0: Chris@0: // Test the image style 'thumbnail' formatter. Chris@0: $display_options['settings']['image_link'] = ''; Chris@0: $display_options['settings']['image_style'] = 'thumbnail'; Chris@0: $display->setComponent($field_name, $display_options) Chris@0: ->save(); Chris@0: Chris@0: // Ensure the derivative image is generated so we do not have to deal with Chris@0: // image style callback paths. Chris@0: $this->drupalGet(ImageStyle::load('thumbnail')->buildUrl($image_uri)); Chris@0: $image_style = [ Chris@0: '#theme' => 'image_style', Chris@0: '#uri' => $image_uri, Chris@0: '#width' => 40, Chris@0: '#height' => 20, Chris@0: '#style_name' => 'thumbnail', Chris@0: '#alt' => $alt, Chris@0: ]; Chris@0: $default_output = $renderer->renderRoot($image_style); Chris@0: $this->drupalGet('node/' . $nid); Chris@0: $image_style = ImageStyle::load('thumbnail'); Chris@0: $this->assertCacheTag($image_style->getCacheTags()[0]); Chris@0: $this->assertRaw($default_output, 'Image style thumbnail formatter displaying correctly on full node view.'); Chris@0: Chris@0: if ($scheme == 'private') { Chris@0: // Log out and try to access the file. Chris@0: $this->drupalLogout(); Chris@0: $this->drupalGet(ImageStyle::load('thumbnail')->buildUrl($image_uri)); Chris@0: $this->assertResponse('403', 'Access denied to image style thumbnail as anonymous user.'); Chris@0: } Chris@0: Chris@0: // Test the image URL formatter without an image style. Chris@0: $display_options = [ Chris@0: 'type' => 'image_url', Chris@0: 'settings' => ['image_style' => ''], Chris@0: ]; Chris@0: $expected_url = file_url_transform_relative(file_create_url($image_uri)); Chris@0: $this->assertEqual($expected_url, $node->{$field_name}->view($display_options)[0]['#markup']); Chris@0: Chris@0: // Test the image URL formatter with an image style. Chris@0: $display_options['settings']['image_style'] = 'thumbnail'; Chris@0: $expected_url = file_url_transform_relative(ImageStyle::load('thumbnail')->buildUrl($image_uri)); Chris@0: $this->assertEqual($expected_url, $node->{$field_name}->view($display_options)[0]['#markup']); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests for image field settings. Chris@0: */ Chris@0: public function testImageFieldSettings() { Chris@0: /** @var \Drupal\Core\Render\RendererInterface $renderer */ Chris@0: $renderer = $this->container->get('renderer'); Chris@0: $node_storage = $this->container->get('entity.manager')->getStorage('node'); Chris@0: $test_image = current($this->drupalGetTestFiles('image')); Chris@0: list(, $test_image_extension) = explode('.', $test_image->filename); Chris@0: $field_name = strtolower($this->randomMachineName()); Chris@0: $field_settings = [ Chris@0: 'alt_field' => 1, Chris@0: 'file_extensions' => $test_image_extension, Chris@0: 'max_filesize' => '50 KB', Chris@0: 'max_resolution' => '100x100', Chris@0: 'min_resolution' => '10x10', Chris@0: 'title_field' => 1, Chris@0: ]; Chris@0: $widget_settings = [ Chris@0: 'preview_image_style' => 'medium', Chris@0: ]; Chris@0: $field = $this->createImageField($field_name, 'article', [], $field_settings, $widget_settings); Chris@0: Chris@0: // Verify that the min/max resolution set on the field are properly Chris@0: // extracted, and displayed, on the image field's configuration form. Chris@0: $this->drupalGet('admin/structure/types/manage/article/fields/' . $field->id()); Chris@0: $this->assertFieldByName('settings[max_resolution][x]', '100', 'Expected max resolution X value of 100.'); Chris@0: $this->assertFieldByName('settings[max_resolution][y]', '100', 'Expected max resolution Y value of 100.'); Chris@0: $this->assertFieldByName('settings[min_resolution][x]', '10', 'Expected min resolution X value of 10.'); Chris@0: $this->assertFieldByName('settings[min_resolution][y]', '10', 'Expected min resolution Y value of 10.'); Chris@0: Chris@0: $this->drupalGet('node/add/article'); Chris@0: $this->assertText(t('50 KB limit.'), 'Image widget max file size is displayed on article form.'); Chris@0: $this->assertText(t('Allowed types: @extensions.', ['@extensions' => $test_image_extension]), 'Image widget allowed file types displayed on article form.'); Chris@0: $this->assertText(t('Images must be larger than 10x10 pixels. Images larger than 100x100 pixels will be resized.'), 'Image widget allowed resolution displayed on article form.'); Chris@0: Chris@0: // We have to create the article first and then edit it because the alt Chris@0: // and title fields do not display until the image has been attached. Chris@0: Chris@0: // Create alt text for the image. Chris@0: $alt = $this->randomMachineName(); Chris@0: Chris@0: $nid = $this->uploadNodeImage($test_image, $field_name, 'article', $alt); Chris@0: $this->drupalGet('node/' . $nid . '/edit'); Chris@0: $this->assertFieldByName($field_name . '[0][alt]', '', 'Alt field displayed on article form.'); Chris@0: $this->assertFieldByName($field_name . '[0][title]', '', 'Title field displayed on article form.'); Chris@0: // Verify that the attached image is being previewed using the 'medium' Chris@0: // style. Chris@0: $node_storage->resetCache([$nid]); Chris@0: $node = $node_storage->load($nid); Chris@0: $file = $node->{$field_name}->entity; Chris@0: Chris@0: $url = file_url_transform_relative(file_create_url(ImageStyle::load('medium')->buildUrl($file->getFileUri()))); Chris@0: $this->assertTrue($this->cssSelect('img[width=40][height=20][class=image-style-medium][src="' . $url . '"]')); Chris@0: Chris@0: // Add alt/title fields to the image and verify that they are displayed. Chris@0: $image = [ Chris@0: '#theme' => 'image', Chris@0: '#uri' => $file->getFileUri(), Chris@0: '#alt' => $alt, Chris@0: '#title' => $this->randomMachineName(), Chris@0: '#width' => 40, Chris@0: '#height' => 20, Chris@0: ]; Chris@0: $edit = [ Chris@0: $field_name . '[0][alt]' => $image['#alt'], Chris@0: $field_name . '[0][title]' => $image['#title'], Chris@0: ]; Chris@0: $this->drupalPostForm('node/' . $nid . '/edit', $edit, t('Save')); Chris@0: $default_output = str_replace("\n", NULL, $renderer->renderRoot($image)); Chris@0: $this->assertRaw($default_output, 'Image displayed using user supplied alt and title attributes.'); Chris@0: Chris@0: // Verify that alt/title longer than allowed results in a validation error. Chris@0: $test_size = 2000; Chris@0: $edit = [ Chris@0: $field_name . '[0][alt]' => $this->randomMachineName($test_size), Chris@0: $field_name . '[0][title]' => $this->randomMachineName($test_size), Chris@0: ]; Chris@0: $this->drupalPostForm('node/' . $nid . '/edit', $edit, t('Save')); Chris@0: $schema = $field->getFieldStorageDefinition()->getSchema(); Chris@0: $this->assertRaw(t('Alternative text cannot be longer than %max characters but is currently %length characters long.', [ Chris@0: '%max' => $schema['columns']['alt']['length'], Chris@0: '%length' => $test_size, Chris@0: ])); Chris@0: $this->assertRaw(t('Title cannot be longer than %max characters but is currently %length characters long.', [ Chris@0: '%max' => $schema['columns']['title']['length'], Chris@0: '%length' => $test_size, Chris@0: ])); Chris@0: Chris@0: // Set cardinality to unlimited and add upload a second image. Chris@0: // The image widget is extending on the file widget, but the image field Chris@0: // type does not have the 'display_field' setting which is expected by Chris@0: // the file widget. This resulted in notices before when cardinality is not Chris@0: // 1, so we need to make sure the file widget prevents these notices by Chris@0: // providing all settings, even if they are not used. Chris@0: // @see FileWidget::formMultipleElements(). Chris@0: $this->drupalPostForm('admin/structure/types/manage/article/fields/node.article.' . $field_name . '/storage', ['cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED], t('Save field settings')); Chris@0: $edit = [ Chris@0: 'files[' . $field_name . '_1][]' => drupal_realpath($test_image->uri), Chris@0: ]; Chris@0: $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save')); Chris@0: // Add the required alt text. Chris@0: $this->drupalPostForm(NULL, [$field_name . '[1][alt]' => $alt], t('Save')); Chris@0: $this->assertText(format_string('Article @title has been updated.', ['@title' => $node->getTitle()])); Chris@0: Chris@0: // Assert ImageWidget::process() calls FieldWidget::process(). Chris@0: $this->drupalGet('node/' . $node->id() . '/edit'); Chris@0: $edit = [ Chris@0: 'files[' . $field_name . '_2][]' => drupal_realpath($test_image->uri), Chris@0: ]; Chris@0: $this->drupalPostAjaxForm(NULL, $edit, $field_name . '_2_upload_button'); Chris@0: $this->assertNoRaw(''); Chris@0: $this->assertRaw(''); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test use of a default image with an image field. Chris@0: */ Chris@0: public function testImageFieldDefaultImage() { Chris@0: /** @var \Drupal\Core\Render\RendererInterface $renderer */ Chris@0: $renderer = $this->container->get('renderer'); Chris@0: Chris@0: $node_storage = $this->container->get('entity.manager')->getStorage('node'); Chris@0: // Create a new image field. Chris@0: $field_name = strtolower($this->randomMachineName()); Chris@0: $this->createImageField($field_name, 'article'); Chris@0: Chris@0: // Create a new node, with no images and verify that no images are Chris@0: // displayed. Chris@0: $node = $this->drupalCreateNode(['type' => 'article']); Chris@0: $this->drupalGet('node/' . $node->id()); Chris@0: // Verify that no image is displayed on the page by checking for the class Chris@0: // that would be used on the image field. Chris@0: $this->assertNoPattern('
', 'No image displayed when no image is attached and no default image specified.'); Chris@0: $cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags'); Chris@0: $this->assertTrue(!preg_match('/ image_style\:/', $cache_tags_header), 'No image style cache tag found.'); Chris@0: Chris@0: // Add a default image to the public image field. Chris@0: $images = $this->drupalGetTestFiles('image'); Chris@0: $alt = $this->randomString(512); Chris@0: $title = $this->randomString(1024); Chris@0: $edit = [ Chris@0: // Get the path of the 'image-test.png' file. Chris@0: 'files[settings_default_image_uuid]' => drupal_realpath($images[0]->uri), Chris@0: 'settings[default_image][alt]' => $alt, Chris@0: 'settings[default_image][title]' => $title, Chris@0: ]; Chris@0: $this->drupalPostForm("admin/structure/types/manage/article/fields/node.article.$field_name/storage", $edit, t('Save field settings')); Chris@0: // Clear field definition cache so the new default image is detected. Chris@0: \Drupal::entityManager()->clearCachedFieldDefinitions(); Chris@0: $field_storage = FieldStorageConfig::loadByName('node', $field_name); Chris@0: $default_image = $field_storage->getSetting('default_image'); Chris@0: $file = \Drupal::entityManager()->loadEntityByUuid('file', $default_image['uuid']); Chris@0: $this->assertTrue($file->isPermanent(), 'The default image status is permanent.'); Chris@0: $image = [ Chris@0: '#theme' => 'image', Chris@0: '#uri' => $file->getFileUri(), Chris@0: '#alt' => $alt, Chris@0: '#title' => $title, Chris@0: '#width' => 40, Chris@0: '#height' => 20, Chris@0: ]; Chris@0: $default_output = str_replace("\n", NULL, $renderer->renderRoot($image)); Chris@0: $this->drupalGet('node/' . $node->id()); Chris@0: $this->assertCacheTag($file->getCacheTags()[0]); Chris@0: $cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags'); Chris@0: $this->assertTrue(!preg_match('/ image_style\:/', $cache_tags_header), 'No image style cache tag found.'); Chris@0: $this->assertRaw($default_output, 'Default image displayed when no user supplied image is present.'); Chris@0: Chris@0: // Create a node with an image attached and ensure that the default image Chris@0: // is not displayed. Chris@0: Chris@0: // Create alt text for the image. Chris@0: $alt = $this->randomMachineName(); Chris@0: Chris@0: // Upload the 'image-test.gif' file. Chris@0: $nid = $this->uploadNodeImage($images[2], $field_name, 'article', $alt); Chris@0: $node_storage->resetCache([$nid]); Chris@0: $node = $node_storage->load($nid); Chris@0: $file = $node->{$field_name}->entity; Chris@0: $image = [ Chris@0: '#theme' => 'image', Chris@0: '#uri' => $file->getFileUri(), Chris@0: '#width' => 40, Chris@0: '#height' => 20, Chris@0: '#alt' => $alt, Chris@0: ]; Chris@0: $image_output = str_replace("\n", NULL, $renderer->renderRoot($image)); Chris@0: $this->drupalGet('node/' . $nid); Chris@0: $this->assertCacheTag($file->getCacheTags()[0]); Chris@0: $cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags'); Chris@0: $this->assertTrue(!preg_match('/ image_style\:/', $cache_tags_header), 'No image style cache tag found.'); Chris@0: $this->assertNoRaw($default_output, 'Default image is not displayed when user supplied image is present.'); Chris@0: $this->assertRaw($image_output, 'User supplied image is displayed.'); Chris@0: Chris@0: // Remove default image from the field and make sure it is no longer used. Chris@0: $edit = [ Chris@0: 'settings[default_image][uuid][fids]' => 0, Chris@0: ]; Chris@0: $this->drupalPostForm("admin/structure/types/manage/article/fields/node.article.$field_name/storage", $edit, t('Save field settings')); Chris@0: // Clear field definition cache so the new default image is detected. Chris@0: \Drupal::entityManager()->clearCachedFieldDefinitions(); Chris@0: $field_storage = FieldStorageConfig::loadByName('node', $field_name); Chris@0: $default_image = $field_storage->getSetting('default_image'); Chris@0: $this->assertFalse($default_image['uuid'], 'Default image removed from field.'); Chris@0: // Create an image field that uses the private:// scheme and test that the Chris@0: // default image works as expected. Chris@0: $private_field_name = strtolower($this->randomMachineName()); Chris@0: $this->createImageField($private_field_name, 'article', ['uri_scheme' => 'private']); Chris@0: // Add a default image to the new field. Chris@0: $edit = [ Chris@0: // Get the path of the 'image-test.gif' file. Chris@0: 'files[settings_default_image_uuid]' => drupal_realpath($images[2]->uri), Chris@0: 'settings[default_image][alt]' => $alt, Chris@0: 'settings[default_image][title]' => $title, Chris@0: ]; Chris@0: $this->drupalPostForm('admin/structure/types/manage/article/fields/node.article.' . $private_field_name . '/storage', $edit, t('Save field settings')); Chris@0: // Clear field definition cache so the new default image is detected. Chris@0: \Drupal::entityManager()->clearCachedFieldDefinitions(); Chris@0: Chris@0: $private_field_storage = FieldStorageConfig::loadByName('node', $private_field_name); Chris@0: $default_image = $private_field_storage->getSetting('default_image'); Chris@0: $file = \Drupal::entityManager()->loadEntityByUuid('file', $default_image['uuid']); Chris@0: $this->assertEqual('private', file_uri_scheme($file->getFileUri()), 'Default image uses private:// scheme.'); Chris@0: $this->assertTrue($file->isPermanent(), 'The default image status is permanent.'); Chris@0: // Create a new node with no image attached and ensure that default private Chris@0: // image is displayed. Chris@0: $node = $this->drupalCreateNode(['type' => 'article']); Chris@0: $image = [ Chris@0: '#theme' => 'image', Chris@0: '#uri' => $file->getFileUri(), Chris@0: '#alt' => $alt, Chris@0: '#title' => $title, Chris@0: '#width' => 40, Chris@0: '#height' => 20, Chris@0: ]; Chris@0: $default_output = str_replace("\n", NULL, $renderer->renderRoot($image)); Chris@0: $this->drupalGet('node/' . $node->id()); Chris@0: $this->assertCacheTag($file->getCacheTags()[0]); Chris@0: $cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags'); Chris@0: $this->assertTrue(!preg_match('/ image_style\:/', $cache_tags_header), 'No image style cache tag found.'); Chris@0: $this->assertRaw($default_output, 'Default private image displayed when no user supplied image is present.'); Chris@0: } Chris@0: Chris@0: }