Chris@0: 'entity_test', Chris@0: 'field_name' => 'image_test', Chris@0: 'type' => 'image', Chris@0: 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, Chris@0: ])->save(); Chris@0: FieldConfig::create([ Chris@0: 'entity_type' => 'entity_test', Chris@0: 'field_name' => 'image_test', Chris@0: 'bundle' => 'entity_test', Chris@0: ])->save(); Chris@0: file_unmanaged_copy(\Drupal::root() . '/core/misc/druplicon.png', 'public://example.jpg'); Chris@0: $this->image = File::create([ Chris@0: 'uri' => 'public://example.jpg', Chris@0: ]); Chris@0: $this->image->save(); Chris@0: $this->imageFactory = $this->container->get('image.factory'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests usage of the image field formatters. Chris@0: */ Chris@0: public function testImageFormatterTheme() { Chris@0: /** @var \Drupal\Core\Render\RendererInterface $renderer */ Chris@0: $renderer = $this->container->get('renderer'); Chris@0: Chris@0: // Create an image. Chris@0: $files = $this->drupalGetTestFiles('image'); Chris@0: $file = reset($files); Chris@0: $original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME); Chris@0: Chris@0: // Create a style. Chris@0: $style = ImageStyle::create(['name' => 'test', 'label' => 'Test']); Chris@0: $style->save(); Chris@0: $url = file_url_transform_relative($style->buildUrl($original_uri)); Chris@0: Chris@0: // Create a test entity with the image field set. Chris@0: $entity = EntityTest::create(); Chris@0: $entity->image_test->target_id = $this->image->id(); Chris@0: $entity->image_test->alt = NULL; Chris@0: $entity->image_test->uri = $original_uri; Chris@0: $image = $this->imageFactory->get('public://example.jpg'); Chris@0: $entity->save(); Chris@0: Chris@0: // Create the base element that we'll use in the tests below. Chris@0: $path = $this->randomMachineName(); Chris@0: $base_element = [ Chris@0: '#theme' => 'image_formatter', Chris@0: '#image_style' => 'test', Chris@0: '#item' => $entity->image_test, Chris@0: '#url' => Url::fromUri('base:' . $path), Chris@0: ]; Chris@0: Chris@0: // Test using theme_image_formatter() with a NULL value for the alt option. Chris@0: $element = $base_element; Chris@0: $this->setRawContent($renderer->renderRoot($element)); Chris@0: $elements = $this->xpath('//a[@href=:path]/img[@class="image-style-test" and @src=:url and @width=:width and @height=:height]', [':path' => base_path() . $path, ':url' => $url, ':width' => $image->getWidth(), ':height' => $image->getHeight()]); Chris@0: $this->assertEqual(count($elements), 1, 'theme_image_formatter() correctly renders with a NULL value for the alt option.'); Chris@0: Chris@0: // Test using theme_image_formatter() without an image title, alt text, or Chris@0: // link options. Chris@0: $element = $base_element; Chris@0: $element['#item']->alt = ''; Chris@0: $this->setRawContent($renderer->renderRoot($element)); Chris@0: $elements = $this->xpath('//a[@href=:path]/img[@class="image-style-test" and @src=:url and @width=:width and @height=:height and @alt=""]', [':path' => base_path() . $path, ':url' => $url, ':width' => $image->getWidth(), ':height' => $image->getHeight()]); Chris@0: $this->assertEqual(count($elements), 1, 'theme_image_formatter() correctly renders without title, alt, or path options.'); Chris@0: Chris@0: // Link the image to a fragment on the page, and not a full URL. Chris@0: $fragment = $this->randomMachineName(); Chris@0: $element = $base_element; Chris@0: $element['#url'] = Url::fromRoute('', [], ['fragment' => $fragment]); Chris@0: $this->setRawContent($renderer->renderRoot($element)); Chris@0: $elements = $this->xpath('//a[@href=:fragment]/img[@class="image-style-test" and @src=:url and @width=:width and @height=:height and @alt=""]', [ Chris@0: ':fragment' => '#' . $fragment, Chris@0: ':url' => $url, Chris@0: ':width' => $image->getWidth(), Chris@0: ':height' => $image->getHeight() Chris@0: ]); Chris@0: $this->assertEqual(count($elements), 1, 'theme_image_formatter() correctly renders a link fragment.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests usage of the image style theme function. Chris@0: */ Chris@0: public function testImageStyleTheme() { Chris@0: /** @var \Drupal\Core\Render\RendererInterface $renderer */ Chris@0: $renderer = $this->container->get('renderer'); Chris@0: Chris@0: // Create an image. Chris@0: $files = $this->drupalGetTestFiles('image'); Chris@0: $file = reset($files); Chris@0: $original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME); Chris@0: Chris@0: // Create a style. Chris@0: $style = ImageStyle::create(['name' => 'image_test', 'label' => 'Test']); Chris@0: $style->save(); Chris@0: $url = file_url_transform_relative($style->buildUrl($original_uri)); Chris@0: Chris@0: // Create the base element that we'll use in the tests below. Chris@0: $base_element = [ Chris@0: '#theme' => 'image_style', Chris@0: '#style_name' => 'image_test', Chris@0: '#uri' => $original_uri, Chris@0: ]; Chris@0: Chris@0: $element = $base_element; Chris@0: $this->setRawContent($renderer->renderRoot($element)); Chris@0: $elements = $this->xpath('//img[@class="image-style-image-test" and @src=:url and @alt=""]', [':url' => $url]); Chris@0: $this->assertEqual(count($elements), 1, 'theme_image_style() renders an image correctly.'); Chris@0: Chris@0: // Test using theme_image_style() with a NULL value for the alt option. Chris@0: $element = $base_element; Chris@0: $element['#alt'] = NULL; Chris@0: $this->setRawContent($renderer->renderRoot($element)); Chris@0: $elements = $this->xpath('//img[@class="image-style-image-test" and @src=:url]', [':url' => $url]); Chris@0: $this->assertEqual(count($elements), 1, 'theme_image_style() renders an image correctly with a NULL value for the alt option.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests image alt attribute functionality. Chris@0: */ Chris@0: public function testImageAltFunctionality() { Chris@0: /** @var \Drupal\Core\Render\RendererInterface $renderer */ Chris@0: $renderer = $this->container->get('renderer'); Chris@0: Chris@0: // Test using alt directly with alt attribute. Chris@0: $image_with_alt_property = [ Chris@0: '#theme' => 'image', Chris@0: '#uri' => '/core/themes/bartik/logo.svg', Chris@0: '#alt' => 'Regular alt', Chris@0: '#title' => 'Test title', Chris@0: '#width' => '50%', Chris@0: '#height' => '50%', Chris@0: '#attributes' => ['class' => 'image-with-regular-alt', 'id' => 'my-img'], Chris@0: ]; Chris@0: Chris@0: $this->setRawContent($renderer->renderRoot($image_with_alt_property)); Chris@0: $elements = $this->xpath('//img[contains(@class, class) and contains(@alt, :alt)]', [":class" => "image-with-regular-alt", ":alt" => "Regular alt"]); Chris@0: $this->assertEqual(count($elements), 1, 'Regular alt displays correctly'); Chris@0: Chris@0: // Test using alt attribute inside attributes. Chris@0: $image_with_alt_attribute_alt_attribute = [ Chris@0: '#theme' => 'image', Chris@0: '#uri' => '/core/themes/bartik/logo.svg', Chris@0: '#width' => '50%', Chris@0: '#height' => '50%', Chris@0: '#attributes' => [ Chris@0: 'class' => 'image-with-attribute-alt', Chris@0: 'id' => 'my-img', Chris@0: 'title' => 'New test title', Chris@0: 'alt' => 'Attribute alt', Chris@0: ], Chris@0: ]; Chris@0: Chris@0: $this->setRawContent($renderer->renderRoot($image_with_alt_attribute_alt_attribute)); Chris@0: $elements = $this->xpath('//img[contains(@class, class) and contains(@alt, :alt)]', [":class" => "image-with-attribute-alt", ":alt" => "Attribute alt"]); Chris@0: $this->assertEqual(count($elements), 1, 'Attribute alt displays correctly'); Chris@0: Chris@0: // Test using alt attribute as property and inside attributes. Chris@0: $image_with_alt_attribute_both = [ Chris@0: '#theme' => 'image', Chris@0: '#uri' => '/core/themes/bartik/logo.svg', Chris@0: '#width' => '50%', Chris@0: '#height' => '50%', Chris@0: '#alt' => 'Kitten sustainable', Chris@0: '#attributes' => [ Chris@0: 'class' => 'image-with-attribute-alt', Chris@0: 'id' => 'my-img', Chris@0: 'title' => 'New test title', Chris@0: 'alt' => 'Attribute alt', Chris@0: ], Chris@0: ]; Chris@0: Chris@0: $this->setRawContent($renderer->renderRoot($image_with_alt_attribute_both)); Chris@0: $elements = $this->xpath('//img[contains(@class, class) and contains(@alt, :alt)]', [":class" => "image-with-attribute-alt", ":alt" => "Attribute alt"]); Chris@0: $this->assertEqual(count($elements), 1, 'Attribute alt overrides alt property if both set.'); Chris@0: } Chris@0: Chris@0: }