You are here

class ViewsIntegrationTest in Drupal 10

Same name in this branch
  1. 10 core/modules/responsive_image/tests/src/Functional/ViewsIntegrationTest.php \Drupal\Tests\responsive_image\Functional\ViewsIntegrationTest
  2. 10 core/modules/dblog/tests/src/Kernel/Views/ViewsIntegrationTest.php \Drupal\Tests\dblog\Kernel\Views\ViewsIntegrationTest
Same name and namespace in other branches
  1. 8 core/modules/responsive_image/tests/src/Functional/ViewsIntegrationTest.php \Drupal\Tests\responsive_image\Functional\ViewsIntegrationTest
  2. 9 core/modules/responsive_image/tests/src/Functional/ViewsIntegrationTest.php \Drupal\Tests\responsive_image\Functional\ViewsIntegrationTest

Tests the integration of responsive image with Views.

@group responsive_image

Hierarchy

  • class \Drupal\Tests\responsive_image\Functional\ViewsIntegrationTest extends \Drupal\Tests\views\Functional\ViewTestBase

Expanded class hierarchy of ViewsIntegrationTest

File

core/modules/responsive_image/tests/src/Functional/ViewsIntegrationTest.php, line 15

Namespace

Drupal\Tests\responsive_image\Functional
View source
class ViewsIntegrationTest extends ViewTestBase {

  /**
   * The responsive image style ID to use.
   */
  const RESPONSIVE_IMAGE_STYLE_ID = 'responsive_image_style_id';

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'views',
    'views_ui',
    'responsive_image',
    'field',
    'image',
    'file',
    'entity_test',
    'breakpoint',
    'responsive_image_test_module',
  ];

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * The test views to enable.
   */
  public static $testViews = [
    'entity_test_row',
  ];

  /**
   * {@inheritdoc}
   */
  protected function setUp($import_test_views = TRUE, $modules = [
    'views_test_config',
  ]) : void {
    parent::setUp($import_test_views, $modules);
    $this
      ->enableViewsTestModule();

    // Create a responsive image style.
    $responsive_image_style = ResponsiveImageStyle::create([
      'id' => self::RESPONSIVE_IMAGE_STYLE_ID,
      'label' => 'Foo',
      'breakpoint_group' => 'responsive_image_test_module',
    ]);

    // Create an image field to be used with a responsive image formatter.
    FieldStorageConfig::create([
      'type' => 'image',
      'entity_type' => 'entity_test',
      'field_name' => 'bar',
    ])
      ->save();
    FieldConfig::create([
      'entity_type' => 'entity_test',
      'bundle' => 'entity_test',
      'field_name' => 'bar',
    ])
      ->save();
    $responsive_image_style
      ->addImageStyleMapping('responsive_image_test_module.mobile', '1x', [
      'image_mapping_type' => 'image_style',
      'image_mapping' => 'thumbnail',
    ])
      ->addImageStyleMapping('responsive_image_test_module.narrow', '1x', [
      'image_mapping_type' => 'image_style',
      'image_mapping' => 'medium',
    ])
      ->addImageStyleMapping('responsive_image_test_module.wide', '1x', [
      'image_mapping_type' => 'image_style',
      'image_mapping' => 'large',
    ])
      ->save();
    $admin_user = $this
      ->drupalCreateUser([
      'administer views',
    ]);
    $this
      ->drupalLogin($admin_user);
  }

  /**
   * Tests integration with Views.
   */
  public function testViewsAddResponsiveImageField() {

    // Add the image field to the View.
    $this
      ->drupalGet('admin/structure/views/nojs/add-handler/entity_test_row/default/field');
    $this
      ->drupalGet('admin/structure/views/nojs/add-handler/entity_test_row/default/field');
    $this
      ->submitForm([
      'name[entity_test__bar.bar]' => TRUE,
    ], 'Add and configure field');

    // Set the formatter to 'Responsive image'.
    $this
      ->submitForm([
      'options[type]' => 'responsive_image',
    ], 'Apply');
    $this
      ->assertSession()
      ->responseContains('Responsive image style field is required.');
    $this
      ->submitForm([
      'options[settings][responsive_image_style]' => self::RESPONSIVE_IMAGE_STYLE_ID,
    ], 'Apply');
    $this
      ->drupalGet('admin/structure/views/nojs/handler/entity_test_row/default/field/bar');

    // Make sure the selected value is set.
    $this
      ->assertSession()
      ->fieldValueEquals('options[settings][responsive_image_style]', self::RESPONSIVE_IMAGE_STYLE_ID);
  }

}

Members