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 public function __construct() {
18 $driver = new DrupalDriver(DRUPAL_ROOT, '');
19 $driver->setCoreFromVersion();
28 public static function cleanDB() {
29 $module_path = \Drupal::service('module_handler')->getModule('permissions_by_term')->getPath();
31 $defaultSitesDirPath = \Drupal::service('stream_wrapper_manager')->getViaScheme(file_default_scheme())->realpath() . '/';
32 unlink($defaultSitesDirPath . 'db.sqlite');
33 copy($module_path . '/tests/src/Behat/fixtures/db.sqlite', $defaultSitesDirPath . 'db.sqlite');
34 chmod($defaultSitesDirPath . '/db.sqlite', 0777);
38 * Creates one or more terms on an existing vocabulary.
40 * Provide term data in the following format:
42 * | name | parent | description | weight | taxonomy_field_image | access_user | access_role |
43 * | Snook | Fish | Marine fish | 10 | snook-123.jpg | Bob | editor |
44 * | ... | ... | ... | ... | ... | ... | ... |
46 * Only the 'name' field is required.
48 * @Given restricted :vocabulary terms:
50 public function createTerms($vocabulary, TableNode $termsTable) {
51 foreach ($termsTable->getHash() as $termsHash) {
52 $term = (object) $termsHash;
53 $term->vocabulary_machine_name = $vocabulary;
54 $this->termCreate($term);
56 $accessStorage = \Drupal::Service('permissions_by_term.access_storage');
57 if (!empty($termsHash['access_user'])) {
58 $userNames = explode(', ', $termsHash['access_user']);
59 foreach ($userNames as $userName) {
60 $accessStorage->addTermPermissionsByUserIds([$accessStorage->getUserIdByName($userName)['uid']], $term->tid);
64 if (!empty($termsHash['access_role'])) {
65 $rolesIds = explode(', ', $termsHash['access_role']);
66 $accessStorage->addTermPermissionsByRoleIds($rolesIds, $term->tid);
72 * @Given /^I create vocabulary with name "([^"]*)" and vid "([^"]*)"$/
74 public function createVocabulary($name, $vid) {
75 $vocabulary = \Drupal::entityQuery('taxonomy_vocabulary')
76 ->condition('vid', $vid)
79 if (empty($vocabulary)) {
80 $vocabulary = Vocabulary::create([
89 * @Then I open open Permissions By Term advanced info
91 public function iOpenOpenPermissionsByTermAdvancedInfo()
93 $this->getSession()->evaluateScript("jQuery('#edit-permissions-by-term-info').attr('open', true);");
97 * @Given /^I create (\d+) nodes of type "([^"]*)"$/
99 public function iCreateNodesOfType($number, $type)
101 for ($i = 0; $i <= $number; $i++) {
102 $node = new \stdClass();
104 $node->title = $this->createRandomString();
105 $node->body = $this->createRandomString();
106 $this->nodeCreate($node);
110 private function createRandomString($length = 10) {
111 return substr(str_shuffle(str_repeat("0123456789abcdefghijklmnopqrstuvwxyz", $length)), 0, $length);
115 * @Given Node access records are rebuild.
117 public function nodeAccessRecordsAreRebuild()
119 node_access_rebuild();
123 * @Then /^wait (\d+) seconds$/
125 public function waitSeconds($secondsNumber)
127 $this->getSession()->wait($secondsNumber * 1000);
131 * @Then /^I select index (\d+) in dropdown named "([^"]*)"$/
133 public function selectIndexInDropdown($index, $name)
135 $this->getSession()->evaluateScript('document.getElementsByName("' . $name . '")[0].selectedIndex = ' . $index . ';');
139 * @Then /^I open node edit form by node title "([^"]*)"$/
140 * @param string $title
142 public function openNodeEditFormByTitle($title)
144 $query = \Drupal::service('database')->select('node_field_data', 'nfd')
145 ->fields('nfd', ['nid'])
146 ->condition('nfd.title', $title);
148 $this->visitPath('/node/' . $query->execute()->fetchField() . '/edit');
152 * @Then /^I open node view by node title "([^"]*)"$/
153 * @param string $title
155 public function openNodeViewByTitle($title)
157 $query = \Drupal::service('database')->select('node_field_data', 'nfd')
158 ->fields('nfd', ['nid'])
159 ->condition('nfd.title', $title);
161 $this->visitPath('/node/' . $query->execute()->fetchField());
165 * @Then /^I scroll to element with id "([^"]*)"$/
168 public function iScrollToElementWithId($id)
170 $this->getSession()->executeScript(
172 var element = document.getElementById('" . $id . "');
173 element.scrollIntoView( true );
179 * @Then /^I check checkbox with id "([^"]*)" by JavaScript$/
182 public function checkCheckboxWithJS($id)
184 $this->getSession()->executeScript(
186 document.getElementById('" . $id . "').checked = true;
192 * @Then /^I check checkbox with id "([^"]*)"$/
195 public function checkCheckbox($id)
197 $page = $this->getSession()->getPage();
198 $selectElement = $page->find('xpath', '//input[@id = "' . $id . '"]');
200 $selectElement->check();
204 * @Then /^I uncheck checkbox with id "([^"]*)"$/
207 public function uncheckCheckbox($id)
209 $page = $this->getSession()->getPage();
210 $selectElement = $page->find('xpath', '//input[@id = "' . $id . '"]');
212 $selectElement->uncheck();
216 * @Then /^I select "([^"]*)" in "([^"]*)"$/
217 * @param string $label
220 public function selectOption($label, $id)
222 $page = $this->getSession()->getPage();
223 $selectElement = $page->find('xpath', '//select[@id = "' . $id . '"]');
224 $selectElement->selectOption($label);