View source
<?php
namespace Drupal\Tests\responsive_preview\Functional;
class ResponsivePreviewToolbarTest extends ResponsivePreviewTestBase {
protected static $modules = [
'toolbar',
];
protected $previewUser;
public function setUp() {
parent::setUp();
$this->previewUser = $this
->drupalCreateUser([
'access responsive preview',
'access toolbar',
]);
}
public function testToolbarIntegration() {
$toolbar_xpath = '//div[@id="toolbar-administration"]/nav[@id="toolbar-bar"]';
$tab_xpath = '//nav[@id="toolbar-bar"]//div[contains(@class, "toolbar-tab-responsive-preview")]';
$devices = array_keys($this
->getDefaultDevices(TRUE));
$this
->drupalGet('');
$this
->assertNoResponsivePreviewLibrary();
$this
->assertNoResponsivePreviewCachesTagAndContexts();
$toolbar_user = $this
->drupalCreateUser([
'access toolbar',
]);
$this
->drupalLogin($toolbar_user);
$this
->assertSession()
->elementExists('xpath', $toolbar_xpath);
$this
->assertSession()
->elementNotExists('xpath', $tab_xpath);
$this
->assertNoResponsivePreviewLibrary();
$this
->drupalLogin($this->previewUser);
$this
->assertSession()
->elementExists('xpath', $toolbar_xpath);
$this
->assertSession()
->elementExists('xpath', $tab_xpath);
$this
->assertResponsivePreviewLibrary();
$this
->assertResponsivePreviewCachesTagAndContexts();
$this
->assertDeviceListEquals($devices);
$admin_user = $this
->drupalCreateUser([
'access responsive preview',
'access toolbar',
'administer responsive preview',
]);
$this
->drupalLogin($admin_user);
$this
->drupalGet('admin/config/user-interface/responsive-preview');
$this
->assertSession()
->elementExists('xpath', $toolbar_xpath);
$this
->assertSession()
->elementNotExists('xpath', $tab_xpath);
$this
->assertNoResponsivePreviewLibrary();
$this
->assertResponsivePreviewCachesTagAndContexts();
$this
->drupalGet('');
$this
->assertSession()
->elementExists('xpath', $toolbar_xpath);
$this
->assertSession()
->elementExists('xpath', $tab_xpath);
$this
->assertResponsivePreviewLibrary();
$this
->assertResponsivePreviewCachesTagAndContexts();
$this
->assertDeviceListEquals($devices);
}
public function testCacheInvalidation() {
$device_storage = \Drupal::entityTypeManager()
->getStorage('responsive_preview_device');
$device_ids = array_keys($this
->getDefaultDevices(TRUE));
$devices = array_combine($device_ids, $device_ids);
$this
->drupalLogin($this->previewUser);
$this
->drupalGet('');
$this
->assertDeviceListEquals($devices);
$devices['large'] = 'large';
$device_storage
->load('large')
->setStatus(1)
->save();
$this
->drupalGet('');
$this
->assertDeviceListEquals($devices);
$devices['new_device'] = 'new_device';
$device_storage
->create([
'id' => 'new_device',
'label' => 'Hello, I am new!',
'status' => '1',
'orientation' => 'landscape',
'dimensions' => [
'width' => '1600',
'height' => '2850',
'dppx' => '2.5',
],
])
->save();
$this
->drupalGet('');
$this
->assertDeviceListEquals($devices);
unset($devices['large'], $devices['new_device']);
$entities = $device_storage
->loadMultiple([
'new_device',
'large',
]);
$device_storage
->delete($entities);
$this
->drupalGet('');
$this
->assertDeviceListEquals($devices);
}
}