View source
<?php
namespace Drupal\Tests\system\FunctionalJavascript;
class OffCanvasTest extends OffCanvasTestBase {
protected $lastDialogClass;
public static $modules = [
'off_canvas_test',
];
protected $defaultTheme = 'stark';
public function testOffCanvasLinks($theme) {
$this
->enableTheme($theme);
$this
->drupalGet('/off-canvas-test-links');
$page = $this
->getSession()
->getPage();
$web_assert = $this
->assertSession();
$web_assert
->elementNotExists('css', '#drupal-off-canvas');
foreach ([
'1',
'2',
'1',
] as $link_index) {
$this
->assertOffCanvasDialog($link_index, 'side');
$header_text = $this
->getOffCanvasDialog()
->find('css', '.ui-dialog-title')
->getText();
if ($link_index == '2') {
$web_assert
->elementExists('css', '.ui-dialog-empty-title');
$this
->assertEquals(' ', $header_text);
$style = $page
->find('css', '.ui-dialog-off-canvas')
->getAttribute('style');
$this
->assertTrue(strstr($style, 'width: 555px;') !== FALSE, 'Dialog width respected.');
$page
->clickLink("Open side panel 1");
$this
->waitForOffCanvasToOpen();
$style = $page
->find('css', '.ui-dialog-off-canvas')
->getAttribute('style');
$this
->assertTrue(strstr($style, 'width: 555px;') === FALSE, 'Dialog width reset to default.');
}
else {
$this
->assertEquals("Thing {$link_index}", $header_text);
$web_assert
->elementNotExists('css', '.ui-dialog-empty-title');
}
}
foreach ([
1,
2,
] as $link_index) {
$this
->assertOffCanvasDialog($link_index, 'top');
$style = $page
->find('css', '.ui-dialog-off-canvas')
->getAttribute('style');
if ($link_index === 1) {
$this
->assertTrue((bool) strstr($style, 'height: auto;'));
}
else {
$this
->assertTrue((bool) strstr($style, 'height: 421px;'));
}
}
$this
->drupalGet('/off-canvas-test-links');
$page
->clickLink('Display more links!');
$this
->waitForOffCanvasToOpen();
$web_assert
->linkExists('Off_canvas link!');
$page
->clickLink('Off_canvas link!');
$this
->waitForOffCanvasToOpen();
$web_assert
->elementTextContains('css', '.ui-dialog[aria-describedby="drupal-off-canvas"]', 'Thing 2 says hello');
$this
->drupalGet('/off-canvas-test-links');
$page
->clickLink("Open side panel 1");
$this
->waitForOffCanvasToOpen();
$page
->clickLink('Display more links!');
$this
->waitForOffCanvasToOpen();
$web_assert
->linkExists('Off_canvas link!');
$page
->clickLink('Off_canvas link!');
$this
->waitForOffCanvasToOpen();
$web_assert
->elementTextContains('css', '.ui-dialog[aria-describedby="drupal-off-canvas"]', 'Thing 2 says hello');
}
public function testNarrowWidth() {
$narrow_width_breakpoint = 768;
$offset = 20;
$height = 800;
$page = $this
->getSession()
->getPage();
$web_assert = $this
->assertSession();
foreach ($this
->getTestThemes() as $theme) {
$this
->enableTheme($theme);
$this
->getSession()
->resizeWindow($narrow_width_breakpoint + $offset, $height);
$this
->drupalGet('/off-canvas-test-links');
$this
->assertFalse($page
->find('css', '.dialog-off-canvas-main-canvas')
->hasAttribute('style'), 'Body not padded on wide page load.');
$page
->clickLink("Open side panel 1");
$this
->waitForOffCanvasToOpen();
$page
->waitFor(10, function ($page) {
return $page
->find('css', '.dialog-off-canvas-main-canvas')
->hasAttribute('style');
});
$web_assert
->elementAttributeContains('css', '.dialog-off-canvas-main-canvas', 'style', 'padding-right');
$this
->getSession()
->resizeWindow($narrow_width_breakpoint - $offset, $height);
$this
->drupalGet('/off-canvas-test-links');
$this
->assertFalse($page
->find('css', '.dialog-off-canvas-main-canvas')
->hasAttribute('style'), 'Body not padded on narrow page load.');
$page
->clickLink("Open side panel 1");
$this
->waitForOffCanvasToOpen();
$this
->assertFalse($page
->find('css', '.dialog-off-canvas-main-canvas')
->hasAttribute('style'), 'Body not padded on narrow page with tray open.');
}
}
protected function assertOffCanvasDialog($link_index, $position) {
$page = $this
->getSession()
->getPage();
$web_assert = $this
->assertSession();
$link_text = "Open {$position} panel {$link_index}";
$page
->clickLink($link_text);
if ($this->lastDialogClass) {
$web_assert
->assertNoElementAfterWait('css', '.' . $this->lastDialogClass);
}
$this
->waitForOffCanvasToOpen($position);
$this->lastDialogClass = "{$position}-{$link_index}";
$web_assert
->pageTextContains("Thing {$link_index} says hello");
$off_canvas_tray = $this
->getOffCanvasDialog();
$this
->assertEquals(TRUE, $off_canvas_tray
->isVisible());
$tray_text = $off_canvas_tray
->findById('drupal-off-canvas')
->getText();
$this
->assertEquals("Thing {$link_index} says hello", $tray_text);
}
}