3 namespace Drupal\Tests\responsive_image\Functional;
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\field\Entity\FieldStorageConfig;
7 use Drupal\responsive_image\Entity\ResponsiveImageStyle;
8 use Drupal\Tests\views\Functional\ViewTestBase;
11 * Tests the integration of responsive image with Views.
13 * @group responsive_image
15 class ViewsIntegrationTest extends ViewTestBase {
18 * The responsive image style ID to use.
20 const RESPONSIVE_IMAGE_STYLE_ID = 'responsive_image_style_id';
25 public static $modules = [
34 'responsive_image_test_module',
38 * The test views to enable.
40 public static $testViews = ['entity_test_row'];
45 protected function setUp($import_test_views = TRUE) {
46 parent::setUp($import_test_views);
48 $this->enableViewsTestModule();
50 // Create a responsive image style.
51 $responsive_image_style = ResponsiveImageStyle::create([
52 'id' => self::RESPONSIVE_IMAGE_STYLE_ID,
54 'breakpoint_group' => 'responsive_image_test_module',
56 // Create an image field to be used with a responsive image formatter.
57 FieldStorageConfig::create([
59 'entity_type' => 'entity_test',
60 'field_name' => 'bar',
63 'entity_type' => 'entity_test',
64 'bundle' => 'entity_test',
65 'field_name' => 'bar',
68 $responsive_image_style
69 ->addImageStyleMapping('responsive_image_test_module.mobile', '1x', [
70 'image_mapping_type' => 'image_style',
71 'image_mapping' => 'thumbnail',
73 ->addImageStyleMapping('responsive_image_test_module.narrow', '1x', [
74 'image_mapping_type' => 'image_style',
75 'image_mapping' => 'medium',
77 // Test the normal output of mapping to an image style.
78 ->addImageStyleMapping('responsive_image_test_module.wide', '1x', [
79 'image_mapping_type' => 'image_style',
80 'image_mapping' => 'large',
84 $admin_user = $this->drupalCreateUser(['administer views']);
85 $this->drupalLogin($admin_user);
89 * Tests integration with Views.
91 public function testViewsAddResponsiveImageField() {
92 // Add the image field to the View.
93 $this->drupalGet('admin/structure/views/nojs/add-handler/entity_test_row/default/field');
94 $this->drupalPostForm('admin/structure/views/nojs/add-handler/entity_test_row/default/field', ['name[entity_test__bar.bar]' => TRUE], 'Add and configure field');
95 // Set the formatter to 'Responsive image'.
96 $this->drupalPostForm(NULL, ['options[type]' => 'responsive_image'], 'Apply');
97 $this->assertSession()
98 ->responseContains('Responsive image style field is required.');
99 $this->drupalPostForm(NULL, ['options[settings][responsive_image_style]' => self::RESPONSIVE_IMAGE_STYLE_ID], 'Apply');
100 $this->drupalGet('admin/structure/views/nojs/handler/entity_test_row/default/field/bar');
101 // Make sure the selected value is set.
102 $this->assertSession()
103 ->fieldValueEquals('options[settings][responsive_image_style]', self::RESPONSIVE_IMAGE_STYLE_ID);