View source
<?php
use Drupal\blazy\Blazy;
use Drupal\blazy\BlazyDefault;
class BlazyTest extends DrupalWebTestCase {
protected $adminUser;
protected $anyUser;
public static function getInfo() {
return [
'name' => 'Blazy tests',
'description' => 'Tests the Blazy PSR-4, configuration and permission controls.',
'group' => 'Blazy',
];
}
public function setUp() {
parent::setUp('file', 'image', 'libraries', 'registry_autoload', 'file_entity', 'media', 'blazy_ui', 'picture');
$this->adminUser = $this
->drupalCreateUser([
'administer blazy',
]);
$this->anyUser = $this
->drupalCreateUser([
'access administration pages',
]);
}
public function testAdminAccess() {
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/config/media/blazy');
$this
->assertResponse(200, 'Administrative permission allows access to administration page.');
$this
->drupalLogout();
$this
->drupalLogin($this->anyUser);
$this
->drupalGet('admin/config/media/blazy');
$this
->assertResponse(403, 'Regular users do not have access to administer Blazy pages.');
}
public function testTypecast() {
$this
->drupalLogin($this->adminUser);
$settings_path = 'admin/config/media/blazy';
$this
->drupalGet($settings_path);
$this
->assertResponse(200, 'Administrative user can reach the "Blazy UI" form.');
$edit['responsive_image'] = TRUE;
$this
->drupalPost($settings_path, $edit, t('Save configuration'));
$this
->assertText(t('The configuration options have been saved.'), 'Blazy config has been updated.');
$config = blazy()
->config();
$this
->assertEqual(blazy()
->config('admin_css', TRUE), TRUE, t('Admin CSS is enabled.'));
$this
->assertEqual(blazy()
->config('responsive_image', FALSE), TRUE, t('Picture support was disabled, now enabled.'));
$this
->assertEqual(blazy()
->config('pages'), BlazyDefault::PAGES, t('Pages for BlazyFilter match default.'));
$this
->assertEqual(blazy()
->config('blazy.saveViewportOffsetDelay'), 50, t('blazy.saveViewportOffsetDelay dot notation value matches 50.'));
$this
->assertTrue(array_key_exists('blazy', $config), t('Blazy JS option array is correctly saved.'));
$this
->assertEqual(variable_get('blazy.settings', []), $config, t('Typecast values match variable_get() values.'));
}
public function testNamespace() {
$id = 'my_id ';
$return_id = Blazy::getHtmlId('blazy', $id);
$this
->assertEqual($return_id, 'my-id', t('Html ID changed from my_id to my-id.'));
}
public function testGetLightboxes() {
$lightboxes = blazy()
->getLightboxes();
$this
->assertTrue(!in_array('nixbox', $lightboxes), 'No lightbox nixbox.');
}
public function testAttach() {
$attach = [
'blazy' => TRUE,
'grid' => 0,
'media' => TRUE,
'ratio' => 'fluid',
'style' => 'column',
];
$attachments = blazy()
->attach($attach);
$this
->assertTrue(array_key_exists('library', $attachments), 'Attachments has library defined.');
$this
->assertTrue(array_key_exists('blazy', $attachments['js'][0]['data']), 'Attachments has blazy settings in JS data.');
$this
->assertTrue(array_search('media', array_column($attachments['library'], 1)) !== FALSE, 'Blazy media is attached.');
$this
->assertTrue(array_search('ratio', array_column($attachments['library'], 1)) !== FALSE, 'Blazy ratio is attached.');
$this
->assertTrue(array_search('load', array_column($attachments['library'], 1)) !== FALSE, 'Blazy library is attached.');
$this
->assertTrue(array_search('column', array_column($attachments['library'], 1)) !== FALSE, 'Blazy column is attached.');
$this
->assertFalse(array_search('grid', array_column($attachments['library'], 1)) === FALSE, 'Blazy grid should NOT be attached.');
}
public function testConfig() {
$blazy = blazy()
->config('blazy');
$this
->assertNotNull($blazy['loadInvisible'], 'blazy.loadInvisible exists');
$admin_css = blazy()
->config('admin_css');
$this
->assertTrue($admin_css, 'Blazy admin CSS is enabled by default.');
$responsive_image = blazy()
->config('responsive_image');
$this
->assertFalse($responsive_image, 'Picture is disabled by default.');
}
}