ExcludeTestCase.php in Image Lazyloader 8
File
src/tests/ExcludeTestCase.php
View source
<?php
namespace Drupal\lazyloader\Tests;
use Drupal\Core\Render\Element;
class ExcludeTestCase extends TestBase {
public function testContentTypeExclude() {
$this
->drupalGet("node/{$this->node->nid}");
$this
->assertLazyloaderEnabled();
$edit['lazyloader_content_types[article]'] = 'article';
$this
->drupalPost("admin/config/media/lazyloader/exclude", $edit, t('Save configuration'));
$this
->drupalGet("node/{$this->node->nid}");
$this
->assertLazyloaderEnabled(FALSE, 'Lazyloader is disabled for page nodes when limited to article content types.');
$edit['lazyloader_content_types[article]'] = FALSE;
$edit['lazyloader_content_types[page]'] = 'page';
$this
->drupalPost("admin/config/media/lazyloader/exclude", $edit, t('Save configuration'));
$this
->drupalGet("node/{$this->node->nid}");
$this
->assertLazyloaderEnabled(TRUE, 'Lazyloader is enabled for page nodes when limited to page content types.');
}
public function testPathExclude() {
$alias = $this->node->path['alias'];
$this
->drupalGet($alias);
$this
->assertLazyloaderEnabled();
$edit['lazyloader_paths'] = $alias;
$this
->drupalPost("admin/config/media/lazyloader/exclude", $edit, t('Save configuration'));
$this
->drupalGet($alias);
$this
->assertLazyloaderEnabled(FALSE, 'Lazyloader disabled for disabled alias');
$edit['lazyloader_paths'] = '*' . substr($alias, 2, 2) . '*';
$this
->drupalPost("admin/config/media/lazyloader/exclude", $edit, t('Save configuration'));
$this
->drupalGet($alias);
$this
->assertLazyloaderEnabled(FALSE, 'Lazyloader disabled for disabled alias with wildcards');
$this
->drupalGet("node/{$this->node->nid}");
$this
->assertLazyloaderEnabled(FALSE, 'Lazyloader is also disabled on internal path if alias with wildcard matches ');
}
public function testFilenameExclude() {
$node = node_view($this->node);
\Drupal::configFactory()
->getEditable('lazyloader.settings')
->set('lazyloader_excluded_filenames', $node['field_images'][0]['#item']['filename'])
->save();
$this
->drupalGet("node/{$this->node->nid}");
foreach (Element::children($node['field_images']) as $image) {
$image = $node['field_images'][$image]['#item'];
$pattern = '/data-echo=".*' . preg_quote($image['filename']) . '/';
if ($image['filename'] !== \Drupal::config('lazyloader.settings')
->get('lazyloader_excluded_filenames')) {
$this
->assertSession()
->responseMatches("{$pattern}");
$this
->assertSession()
->responseMatches("{$pattern}", 'Image is lazyloaded when not excluded by filename.');
}
else {
$this
->assertSession()
->responseNotMatches("{$pattern}", 'Image is NOT lazyloaded when excluded by filename.');
}
}
}
public function testImageStyleExclude() {
$this
->drupalGet("node/{$this->node->nid}");
$this
->assertLazyloaderEnabled();
$edit['lazyloader_image_styles[medium]'] = 'medium';
$this
->drupalPost("admin/config/media/lazyloader/exclude", $edit, t('Save configuration'));
$this
->drupalGet("node/{$this->node->nid}");
$this
->assertLazyloaderEnabled(TRUE, 'Lazyloader is enabled for <em>medium</em> image style.');
$edit['lazyloader_image_styles[medium]'] = FALSE;
$edit['lazyloader_image_styles[large]'] = 'large';
$this
->drupalPost("admin/config/media/lazyloader/exclude", $edit, t('Save configuration'));
$this
->drupalGet("node/{$this->node->nid}");
$this
->assertLazyloaderEnabled(FALSE, 'Lazyloader is disabled for <em>medium</em> image style.');
}
}