3 namespace Drupal\Tests\permissions_by_term\Behat\Context;
5 use Behat\Gherkin\Node\TableNode;
6 use Drupal\Driver\DrupalDriver;
7 use Drupal\DrupalExtension\Context\RawDrupalContext;
8 use Drupal\taxonomy\Entity\Vocabulary;
11 * Class PermissionsByTermContext
13 * @package PermissionsByTerm
15 class PermissionsByTermContext extends RawDrupalContext {
17 private const MAX_DURATION_SECONDS = 1200;
19 public function __construct() {
20 $driver = new DrupalDriver(DRUPAL_ROOT, '');
21 $driver->setCoreFromVersion();
28 * Creates one or more terms on an existing vocabulary.
30 * Provide term data in the following format:
32 * | name | parent | description | weight | taxonomy_field_image | access_user | access_role |
33 * | Snook | Fish | Marine fish | 10 | snook-123.jpg | Bob | editor |
34 * | ... | ... | ... | ... | ... | ... | ... |
36 * Only the 'name' field is required.
38 * @Given restricted :vocabulary terms:
40 public function createTerms($vocabulary, TableNode $termsTable) {
41 foreach ($termsTable->getHash() as $termsHash) {
42 $term = (object) $termsHash;
43 $term->vocabulary_machine_name = $vocabulary;
44 $this->termCreate($term);
46 $accessStorage = \Drupal::Service('permissions_by_term.access_storage');
47 if (!empty($termsHash['access_user'])) {
48 $userNames = explode(', ', $termsHash['access_user']);
49 foreach ($userNames as $userName) {
50 $accessStorage->addTermPermissionsByUserIds([$accessStorage->getUserIdByName($userName)['uid']], $term->tid);
54 if (!empty($termsHash['access_role'])) {
55 $rolesIds = explode(', ', $termsHash['access_role']);
56 $accessStorage->addTermPermissionsByRoleIds($rolesIds, $term->tid);
62 * @Given /^I create vocabulary with name "([^"]*)" and vid "([^"]*)"$/
64 public function createVocabulary($name, $vid) {
65 $vocabulary = \Drupal::entityQuery('taxonomy_vocabulary')
66 ->condition('vid', $vid)
69 if (empty($vocabulary)) {
70 $vocabulary = Vocabulary::create([
79 * @Then I open open Permissions By Term advanced info
81 public function iOpenOpenPermissionsByTermAdvancedInfo()
83 $this->getSession()->evaluateScript("jQuery('#edit-permissions-by-term-info').attr('open', true);");
87 * @Given /^I create (\d+) nodes of type "([^"]*)"$/
89 public function iCreateNodesOfType($number, $type)
91 for ($i = 0; $i <= $number; $i++) {
92 $node = new \stdClass();
94 $node->title = $this->createRandomString();
95 $node->body = $this->createRandomString();
96 $this->nodeCreate($node);
100 private function createRandomString($length = 10) {
101 return substr(str_shuffle(str_repeat("0123456789abcdefghijklmnopqrstuvwxyz", $length)), 0, $length);
105 * @Given Node access records are rebuild.
107 public function nodeAccessRecordsAreRebuild()
109 node_access_rebuild();
113 * @Then /^wait (\d+) seconds$/
115 public function waitSeconds($secondsNumber)
117 $this->getSession()->wait($secondsNumber * 1000);
121 * @Then /^I select index (\d+) in dropdown named "([^"]*)"$/
123 public function selectIndexInDropdown($index, $name)
125 $this->getSession()->evaluateScript('document.getElementsByName("' . $name . '")[0].selectedIndex = ' . $index . ';');
129 * @Then /^I open node edit form by node title "([^"]*)"$/
130 * @param string $title
132 public function openNodeEditFormByTitle($title)
134 $query = \Drupal::service('database')->select('node_field_data', 'nfd')
135 ->fields('nfd', ['nid'])
136 ->condition('nfd.title', $title);
138 $this->visitPath('/node/' . $query->execute()->fetchField() . '/edit');
142 * @Then /^I open node view by node title "([^"]*)"$/
143 * @param string $title
145 public function openNodeViewByTitle($title)
147 $query = \Drupal::service('database')->select('node_field_data', 'nfd')
148 ->fields('nfd', ['nid'])
149 ->condition('nfd.title', $title);
151 $this->visitPath('/node/' . $query->execute()->fetchField());
155 * @Then /^I scroll to element with id "([^"]*)"$/
158 public function iScrollToElementWithId($id)
160 $this->getSession()->executeScript(
162 var element = document.getElementById('" . $id . "');
163 element.scrollIntoView( true );
169 * @Then /^I check checkbox with id "([^"]*)" by JavaScript$/
172 public function checkCheckboxWithJS($id)
174 $this->getSession()->executeScript(
176 document.getElementById('" . $id . "').checked = true;
182 * @Then /^I check checkbox with id "([^"]*)"$/
185 public function checkCheckbox($id)
187 $page = $this->getSession()->getPage();
188 $selectElement = $page->find('xpath', '//input[@id = "' . $id . '"]');
190 $selectElement->check();
194 * @Then /^I uncheck checkbox with id "([^"]*)"$/
197 public function uncheckCheckbox($id)
199 $page = $this->getSession()->getPage();
200 $selectElement = $page->find('xpath', '//input[@id = "' . $id . '"]');
202 $selectElement->uncheck();
206 * @Then /^I select "([^"]*)" in "([^"]*)"$/
207 * @param string $label
210 public function selectOption($label, $id)
212 $page = $this->getSession()->getPage();
213 $selectElement = $page->find('xpath', '//select[@id = "' . $id . '"]');
214 $selectElement->selectOption($label);
218 * @Then /^I should see text matching "([^"]*)" after a while$/
220 public function iShouldSeeTextAfterAWhile($text)
225 $content = $this->getSession()->getPage()->getText();
226 if (substr_count($content, $text) > 0) {
229 } while (time() - $startTime < self::MAX_DURATION_SECONDS);
230 throw new ResponseTextException(
231 sprintf('Could not find text %s after %s seconds', $text, self::MAX_DURATION_SECONDS),
234 } catch (StaleElementReference $e) {
240 * @Then /^I click by selector "([^"]*)" via JavaScript$/
241 * @param string $selector
243 public function clickBySelector(string $selector)
245 $this->getSession()->executeScript("document.querySelector('" . $selector . "').click()");