Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / responsive_image / tests / src / Functional / ViewsIntegrationTest.php
1 <?php
2
3 namespace Drupal\Tests\responsive_image\Functional;
4
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;
9
10 /**
11  * Tests the integration of responsive image with Views.
12  *
13  * @group responsive_image
14  */
15 class ViewsIntegrationTest extends ViewTestBase {
16
17   /**
18    * The responsive image style ID to use.
19    */
20   const RESPONSIVE_IMAGE_STYLE_ID = 'responsive_image_style_id';
21
22   /**
23    * {@inheritdoc}
24    */
25   public static $modules = [
26     'views',
27     'views_ui',
28     'responsive_image',
29     'field',
30     'image',
31     'file',
32     'entity_test',
33     'breakpoint',
34     'responsive_image_test_module',
35   ];
36
37   /**
38    * The test views to enable.
39    */
40   public static $testViews = ['entity_test_row'];
41
42   /**
43    * {@inheritdoc}
44    */
45   protected function setUp($import_test_views = TRUE) {
46     parent::setUp($import_test_views);
47
48     $this->enableViewsTestModule();
49
50     // Create a responsive image style.
51     $responsive_image_style = ResponsiveImageStyle::create([
52       'id' => self::RESPONSIVE_IMAGE_STYLE_ID,
53       'label' => 'Foo',
54       'breakpoint_group' => 'responsive_image_test_module',
55     ]);
56     // Create an image field to be used with a responsive image formatter.
57     FieldStorageConfig::create([
58       'type' => 'image',
59       'entity_type' => 'entity_test',
60       'field_name' => 'bar',
61     ])->save();
62     FieldConfig::create([
63       'entity_type' => 'entity_test',
64       'bundle' => 'entity_test',
65       'field_name' => 'bar',
66     ])->save();
67
68     $responsive_image_style
69       ->addImageStyleMapping('responsive_image_test_module.mobile', '1x', [
70         'image_mapping_type' => 'image_style',
71         'image_mapping' => 'thumbnail',
72       ])
73       ->addImageStyleMapping('responsive_image_test_module.narrow', '1x', [
74         'image_mapping_type' => 'image_style',
75         'image_mapping' => 'medium',
76       ])
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',
81       ])
82       ->save();
83
84     $admin_user = $this->drupalCreateUser(['administer views']);
85     $this->drupalLogin($admin_user);
86   }
87
88   /**
89    * Tests integration with Views.
90    */
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);
104   }
105
106 }