View source
<?php
namespace Drupal\responsive_image\Tests;
use Drupal\simpletest\WebTestBase;
class ResponsiveImageAdminUITest extends WebTestBase {
public static $modules = array(
'responsive_image',
'responsive_image_test_module',
);
protected function setUp() {
parent::setUp();
$this
->drupalLogin($this
->drupalCreateUser([
'administer responsive images',
]));
}
public function testResponsiveImageAdmin() {
$this
->drupalGet('admin/config/media/responsive-image-style');
$this
->assertText('There is no Responsive image style yet.');
$this
->drupalGet('admin/config/media/responsive-image-style/add');
$this
->assertFieldByName('breakpoint_group', 'responsive_image');
$edit = array(
'label' => 'Style One',
'id' => 'style_one',
'breakpoint_group' => 'responsive_image_test_module',
'fallback_image_style' => 'thumbnail',
);
$this
->drupalPostForm('admin/config/media/responsive-image-style/add', $edit, t('Save'));
$this
->assertResponse(200);
$this
->drupalGet('admin/config/media/responsive-image-style');
$this
->assertNoText('There is no Responsive image style yet.');
$this
->assertText('Style One');
$this
->assertText('style_one');
$this
->drupalGet('admin/config/media/responsive-image-style/style_one');
$this
->assertFieldByName('label', 'Style One');
$this
->assertFieldByName('breakpoint_group', 'responsive_image_test_module');
$this
->assertFieldByName('fallback_image_style', 'thumbnail');
$cases = array(
array(
'mobile',
'1x',
),
array(
'mobile',
'2x',
),
array(
'narrow',
'1x',
),
array(
'narrow',
'2x',
),
array(
'wide',
'1x',
),
array(
'wide',
'2x',
),
);
$image_styles = array_merge([
RESPONSIVE_IMAGE_EMPTY_IMAGE,
RESPONSIVE_IMAGE_ORIGINAL_IMAGE,
], array_keys(image_style_options(FALSE)));
foreach ($cases as $case) {
$this
->assertFieldByName('keyed_styles[responsive_image_test_module.' . $case[0] . '][' . $case[1] . '][image_mapping_type]', '');
$this
->assertFieldByName('keyed_styles[responsive_image_test_module.' . $case[0] . '][' . $case[1] . '][image_style]', '');
$this
->assertFieldByName('keyed_styles[responsive_image_test_module.' . $case[0] . '][' . $case[1] . '][sizes]', '');
foreach ($image_styles as $image_style_name) {
$this
->assertTrue($this
->xpath('//select[@name=:name]//option[@value=:style]', [
':name' => 'keyed_styles[responsive_image_test_module.' . $case[0] . '][' . $case[1] . '][image_style]',
':style' => $image_style_name,
]));
$this
->assertFieldByName('keyed_styles[responsive_image_test_module.' . $case[0] . '][' . $case[1] . '][sizes_image_styles][' . $image_style_name . ']');
}
}
$edit = array(
'label' => 'Style One',
'breakpoint_group' => 'responsive_image_test_module',
'fallback_image_style' => 'thumbnail',
'keyed_styles[responsive_image_test_module.mobile][1x][image_mapping_type]' => 'image_style',
'keyed_styles[responsive_image_test_module.mobile][1x][image_style]' => 'thumbnail',
'keyed_styles[responsive_image_test_module.narrow][1x][image_mapping_type]' => 'sizes',
'keyed_styles[responsive_image_test_module.narrow][1x][sizes]' => '(min-width: 700px) 700px, 100vw',
'keyed_styles[responsive_image_test_module.narrow][1x][sizes_image_styles][large]' => 'large',
'keyed_styles[responsive_image_test_module.narrow][1x][sizes_image_styles][medium]' => 'medium',
'keyed_styles[responsive_image_test_module.wide][1x][image_mapping_type]' => 'image_style',
'keyed_styles[responsive_image_test_module.wide][1x][image_style]' => 'large',
);
$this
->drupalPostForm('admin/config/media/responsive-image-style/style_one', $edit, t('Save'));
$this
->drupalGet('admin/config/media/responsive-image-style/style_one');
$this
->assertFieldByName('keyed_styles[responsive_image_test_module.mobile][1x][image_style]', 'thumbnail');
$this
->assertFieldByName('keyed_styles[responsive_image_test_module.mobile][1x][image_mapping_type]', 'image_style');
$this
->assertFieldByName('keyed_styles[responsive_image_test_module.mobile][2x][image_mapping_type]', '_none');
$this
->assertFieldByName('keyed_styles[responsive_image_test_module.narrow][1x][image_mapping_type]', 'sizes');
$this
->assertFieldByName('keyed_styles[responsive_image_test_module.narrow][1x][sizes]', '(min-width: 700px) 700px, 100vw');
$this
->assertFieldChecked('edit-keyed-styles-responsive-image-test-modulenarrow-1x-sizes-image-styles-large');
$this
->assertFieldChecked('edit-keyed-styles-responsive-image-test-modulenarrow-1x-sizes-image-styles-medium');
$this
->assertNoFieldChecked('edit-keyed-styles-responsive-image-test-modulenarrow-1x-sizes-image-styles-thumbnail');
$this
->assertFieldByName('keyed_styles[responsive_image_test_module.narrow][2x][image_mapping_type]', '_none');
$this
->assertFieldByName('keyed_styles[responsive_image_test_module.wide][1x][image_style]', 'large');
$this
->assertFieldByName('keyed_styles[responsive_image_test_module.wide][1x][image_mapping_type]', 'image_style');
$this
->assertFieldByName('keyed_styles[responsive_image_test_module.wide][2x][image_mapping_type]', '_none');
$this
->drupalGet('admin/config/media/responsive-image-style/style_one/delete');
$this
->drupalPostForm(NULL, array(), t('Delete'));
$this
->drupalGet('admin/config/media/responsive-image-style');
$this
->assertText('There is no Responsive image style yet.');
}
}