View source
<?php
namespace Drupal\Tests\permissions_by_term\Behat\Context;
use Behat\Gherkin\Node\TableNode;
use Drupal\Driver\DrupalDriver;
use Drupal\DrupalExtension\Context\RawDrupalContext;
use Drupal\taxonomy\Entity\Vocabulary;
class PermissionsByTermContext extends RawDrupalContext {
private const MAX_DURATION_SECONDS = 1200;
public function __construct() {
$driver = new DrupalDriver(DRUPAL_ROOT, '');
$driver
->setCoreFromVersion();
$driver
->bootstrap();
}
public function createTerms($vocabulary, TableNode $termsTable) {
foreach ($termsTable
->getHash() as $termsHash) {
$term = (object) $termsHash;
$term->vocabulary_machine_name = $vocabulary;
$this
->termCreate($term);
$accessStorage = \Drupal::Service('permissions_by_term.access_storage');
if (!empty($termsHash['access_user'])) {
$userNames = explode(', ', $termsHash['access_user']);
foreach ($userNames as $userName) {
$accessStorage
->addTermPermissionsByUserIds([
$accessStorage
->getUserIdByName($userName)['uid'],
], $term->tid);
}
}
if (!empty($termsHash['access_role'])) {
$rolesIds = explode(', ', $termsHash['access_role']);
$accessStorage
->addTermPermissionsByRoleIds($rolesIds, $term->tid);
}
}
}
public function createVocabulary($name, $vid) {
$vocabulary = \Drupal::entityQuery('taxonomy_vocabulary')
->condition('vid', $vid)
->execute();
if (empty($vocabulary)) {
$vocabulary = Vocabulary::create([
'name' => $name,
'vid' => $vid,
]);
$vocabulary
->save();
}
}
public function iOpenOpenPermissionsByTermAdvancedInfo() {
$this
->getSession()
->evaluateScript("jQuery('#edit-permissions-by-term-info').attr('open', true);");
}
public function iCreateNodesOfType($number, $type) {
for ($i = 0; $i <= $number; $i++) {
$node = new \stdClass();
$node->type = $type;
$node->title = $this
->createRandomString();
$node->body = $this
->createRandomString();
$this
->nodeCreate($node);
}
}
private function createRandomString($length = 10) {
return substr(str_shuffle(str_repeat("0123456789abcdefghijklmnopqrstuvwxyz", $length)), 0, $length);
}
public function nodeAccessRecordsAreRebuild() {
node_access_rebuild();
}
public function waitSeconds($secondsNumber) {
$this
->getSession()
->wait($secondsNumber * 1000);
}
public function selectIndexInDropdown($index, $name) {
$this
->getSession()
->evaluateScript('document.getElementsByName("' . $name . '")[0].selectedIndex = ' . $index . ';');
}
public function openNodeEditFormByTitle($title) {
$query = \Drupal::service('database')
->select('node_field_data', 'nfd')
->fields('nfd', [
'nid',
])
->condition('nfd.title', $title);
$this
->visitPath('/node/' . $query
->execute()
->fetchField() . '/edit');
}
public function openNodeViewByTitle($title) {
$query = \Drupal::service('database')
->select('node_field_data', 'nfd')
->fields('nfd', [
'nid',
])
->condition('nfd.title', $title);
$this
->visitPath('/node/' . $query
->execute()
->fetchField());
}
public function iScrollToElementWithId($id) {
$this
->getSession()
->executeScript("\n var element = document.getElementById('" . $id . "');\n element.scrollIntoView( true );\n ");
}
public function checkCheckboxWithJS($id) {
$this
->getSession()
->executeScript("\n document.getElementById('" . $id . "').checked = true;\n ");
}
public function checkCheckbox($id) {
$page = $this
->getSession()
->getPage();
$selectElement = $page
->find('xpath', '//input[@id = "' . $id . '"]');
$selectElement
->check();
}
public function uncheckCheckbox($id) {
$page = $this
->getSession()
->getPage();
$selectElement = $page
->find('xpath', '//input[@id = "' . $id . '"]');
$selectElement
->uncheck();
}
public function selectOption($label, $id) {
$page = $this
->getSession()
->getPage();
$selectElement = $page
->find('xpath', '//select[@id = "' . $id . '"]');
$selectElement
->selectOption($label);
}
public function iShouldSeeTextAfterAWhile($text) {
try {
$startTime = time();
do {
$content = $this
->getSession()
->getPage()
->getText();
if (substr_count($content, $text) > 0) {
return true;
}
} while (time() - $startTime < self::MAX_DURATION_SECONDS);
throw new ResponseTextException(sprintf('Could not find text %s after %s seconds', $text, self::MAX_DURATION_SECONDS), $this
->getSession());
} catch (StaleElementReference $e) {
return true;
}
}
public function clickBySelector(string $selector) {
$this
->getSession()
->executeScript("document.querySelector('" . $selector . "').click()");
}
}