View source
<?php
namespace Acquia\LightningExtension\Context;
use Behat\Mink\Element\DocumentElement;
use Behat\Mink\Element\NodeElement;
use Behat\Mink\Exception\ElementNotFoundException;
use Behat\Mink\Exception\ExpectationException;
use Behat\MinkExtension\Context\RawMinkContext;
use Drupal\DrupalDriverManager;
use Drupal\DrupalExtension\Context\DrupalSubContextInterface;
final class ModerationContext extends RawMinkContext implements DrupalSubContextInterface {
public function __construct(DrupalDriverManager $drupal) {
}
public function openModerationSidebar() {
$this
->assertSession()
->elementExists('css', '#toolbar-bar')
->clickLink('Tasks');
$session = $this
->getSession();
$sidebar = $session
->getPage()
->waitFor(10, function (DocumentElement $page) {
return $page
->find('css', '.moderation-sidebar-container');
});
if (empty($sidebar)) {
throw new ElementNotFoundException($session
->getDriver(), 'element', 'css', '.moderation-sidebar-container');
}
}
public function assertCurrentModerationState($state) {
$assert_session = $this
->assertSession();
$toolbar = $assert_session
->elementExists('css', '#toolbar-bar');
$current_state = $assert_session
->elementExists('named', [
'link',
'Tasks',
], $toolbar)
->getAttribute('data-label');
if ($current_state !== $state) {
throw new ExpectationException("Expected the current moderation state to be {$state}, but it is {$current_state}.", $this
->getSession()
->getDriver());
}
}
public function assertModerationDashboard() {
$this
->assertBlock('views_block:content_moderation_dashboard_in_review-block_1');
$this
->assertBlock('views_block:content_moderation_dashboard_in_review-block_2');
$this
->assertBlock('moderation_dashboard_activity');
$this
->assertBlock('views_block:moderation_dashboard_recently_created-block_1');
$this
->assertBlock('views_block:content_moderation_dashboard_in_review-block_3');
$this
->assertBlock('views_block:moderation_dashboard_recent_changes-block_1');
$this
->assertBlock('views_block:moderation_dashboard_recent_changes-block_2');
$this
->assertBlock('views_block:moderation_dashboard_recently_created-block_2');
}
public function awaitAutosave() {
$driver = $this
->getSession()
->getDriver();
$element = $this
->assertSession()
->elementExists('css', '#autosave-notification');
$is_visible = $element
->waitFor(20, function (NodeElement $element) {
return $element
->isVisible();
});
if ($is_visible == FALSE) {
throw new ExpectationException('Expected autosave notification to appear, but it did not.', $driver);
}
$is_hidden = $element
->waitFor(10, function (NodeElement $element) {
return $element
->isVisible() === FALSE;
});
if ($is_hidden == FALSE) {
throw new ExpectationException('Expected autosave notification to disappear after saving, but it did not.', $driver);
}
}
public function assertRestoreFromAutosave() {
$session = $this
->getSession();
$button = $session
->getPage()
->waitFor(10, function (DocumentElement $page) {
return $page
->findButton('Resume editing');
});
if ($button) {
$button
->press();
}
else {
throw new ElementNotFoundException($session
->getDriver(), 'button', 'named', 'Resume editing');
}
}
public function compareRevisions($a, $b) {
$page = $this
->getSession()
->getPage();
$re = '/^[0-9]+(st|nd|rd|th)$/i';
if (preg_match($re, $a)) {
$a = substr($a, 0, -2);
}
if (preg_match($re, $b)) {
$b = substr($b, 0, -2);
}
$a = (int) $a - 1;
$b = (int) $b - 1;
$page
->clickLink('Revisions');
$rows = $page
->findAll('css', '.diff-revisions tbody tr');
$rows = array_reverse($rows);
$a = $rows[$a]
->findField('radios_left')
->getValue();
$b = $rows[$b]
->findField('radios_right')
->getValue();
$page
->selectFieldOption('radios_left', $a);
$page
->selectFieldOption('radios_right', $b);
$page
->pressButton('Compare');
}
public function assertQuickEditEnabled() {
$session = $this
->getSession();
$is_enabled = $session
->wait(10000, 'Drupal.quickedit.collections.entities.length > 0');
if (empty($is_enabled)) {
throw new ExpectationException('Expected Quick Edit to be enabled, but it is not.', $session
->getDriver());
}
}
public function assertQuickEditDisabled() {
$session = $this
->getSession();
$is_disabled = $session
->wait(10000, 'Drupal.quickedit.collections.entities.length === 0');
if (empty($is_disabled)) {
throw new ExpectationException('Expected Quick Edit to be disabled, but it is not.', $session
->getDriver());
}
}
public function assertQuickEditableBlock($plugin) {
$block = $this
->assertBlock($plugin);
$assert = $this
->assertSession();
$links = $assert
->elementExists('css', 'ul.contextual-links', $block);
$assert
->elementExists('named', [
'link',
'Quick edit',
], $links);
}
private function assertBlock($plugin_id) {
return $this
->assertSession()
->elementExists('css', '[data-block-plugin-id="' . $plugin_id . '"]');
}
}