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