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