Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / permissions_by_term / tests / src / Behat / Context / PermissionsByTermContext.php
1 <?php
2
3 namespace Drupal\Tests\permissions_by_term\Behat\Context;
4
5 use Behat\Gherkin\Node\TableNode;
6 use Drupal\Driver\DrupalDriver;
7 use Drupal\DrupalExtension\Context\RawDrupalContext;
8 use Drupal\taxonomy\Entity\Vocabulary;
9
10 /**
11  * Class PermissionsByTermContext
12  *
13  * @package PermissionsByTerm
14  */
15 class PermissionsByTermContext extends RawDrupalContext {
16
17   private const MAX_DURATION_SECONDS = 1200;
18
19   public function __construct() {
20     $driver = new DrupalDriver(DRUPAL_ROOT, '');
21     $driver->setCoreFromVersion();
22
23     // Bootstrap Drupal.
24     $driver->bootstrap();
25   }
26
27   /**
28    * Creates one or more terms on an existing vocabulary.
29    *
30    * Provide term data in the following format:
31    *
32    * | name  | parent | description | weight | taxonomy_field_image | access_user | access_role |
33    * | Snook | Fish   | Marine fish | 10     | snook-123.jpg        | Bob         | editor      |
34    * | ...   | ...    | ...         | ...    | ...                  | ...         | ...         |
35    *
36    * Only the 'name' field is required.
37    *
38    * @Given restricted :vocabulary terms:
39    */
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);
45
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);
51         }
52       }
53
54       if (!empty($termsHash['access_role'])) {
55         $rolesIds = explode(', ', $termsHash['access_role']);
56         $accessStorage->addTermPermissionsByRoleIds($rolesIds, $term->tid);
57       }
58     }
59   }
60
61   /**
62    * @Given /^I create vocabulary with name "([^"]*)" and vid "([^"]*)"$/
63    */
64   public function createVocabulary($name, $vid) {
65     $vocabulary = \Drupal::entityQuery('taxonomy_vocabulary')
66       ->condition('vid', $vid)
67       ->execute();
68
69     if (empty($vocabulary)) {
70       $vocabulary = Vocabulary::create([
71         'name' => $name,
72         'vid' => $vid,
73       ]);
74       $vocabulary->save();
75     }
76   }
77
78   /**
79    * @Then I open open Permissions By Term advanced info
80    */
81   public function iOpenOpenPermissionsByTermAdvancedInfo()
82   {
83     $this->getSession()->evaluateScript("jQuery('#edit-permissions-by-term-info').attr('open', true);");
84   }
85
86   /**
87    * @Given /^I create (\d+) nodes of type "([^"]*)"$/
88    */
89   public function iCreateNodesOfType($number, $type)
90   {
91     for ($i = 0; $i <= $number; $i++) {
92       $node = new \stdClass();
93       $node->type = $type;
94       $node->title = $this->createRandomString();
95       $node->body = $this->createRandomString();
96       $this->nodeCreate($node);
97     }
98   }
99
100   private function createRandomString($length = 10) {
101     return substr(str_shuffle(str_repeat("0123456789abcdefghijklmnopqrstuvwxyz", $length)), 0, $length);
102   }
103
104   /**
105    * @Given Node access records are rebuild.
106    */
107   public function nodeAccessRecordsAreRebuild()
108   {
109     node_access_rebuild();
110   }
111
112   /**
113    * @Then /^wait (\d+) seconds$/
114    */
115   public function waitSeconds($secondsNumber)
116   {
117     $this->getSession()->wait($secondsNumber * 1000);
118   }
119
120   /**
121    * @Then /^I select index (\d+) in dropdown named "([^"]*)"$/
122    */
123   public function selectIndexInDropdown($index, $name)
124   {
125     $this->getSession()->evaluateScript('document.getElementsByName("' . $name . '")[0].selectedIndex = ' . $index . ';');
126   }
127
128   /**
129    * @Then /^I open node edit form by node title "([^"]*)"$/
130    * @param string $title
131    */
132   public function openNodeEditFormByTitle($title)
133   {
134     $query = \Drupal::service('database')->select('node_field_data', 'nfd')
135       ->fields('nfd', ['nid'])
136       ->condition('nfd.title', $title);
137
138     $this->visitPath('/node/' . $query->execute()->fetchField() . '/edit');
139   }
140
141   /**
142    * @Then /^I open node view by node title "([^"]*)"$/
143    * @param string $title
144    */
145   public function openNodeViewByTitle($title)
146   {
147     $query = \Drupal::service('database')->select('node_field_data', 'nfd')
148       ->fields('nfd', ['nid'])
149       ->condition('nfd.title', $title);
150
151     $this->visitPath('/node/' . $query->execute()->fetchField());
152   }
153
154   /**
155    * @Then /^I scroll to element with id "([^"]*)"$/
156    * @param string $id
157    */
158   public function iScrollToElementWithId($id)
159   {
160     $this->getSession()->executeScript(
161       "
162                 var element = document.getElementById('" . $id . "');
163                 element.scrollIntoView( true );
164             "
165     );
166   }
167
168   /**
169    * @Then /^I check checkbox with id "([^"]*)" by JavaScript$/
170    * @param string $id
171    */
172   public function checkCheckboxWithJS($id)
173   {
174     $this->getSession()->executeScript(
175       "
176                 document.getElementById('" . $id . "').checked = true;
177             "
178     );
179   }
180
181   /**
182    * @Then /^I check checkbox with id "([^"]*)"$/
183    * @param string $id
184    */
185   public function checkCheckbox($id)
186   {
187     $page          = $this->getSession()->getPage();
188     $selectElement = $page->find('xpath', '//input[@id = "' . $id . '"]');
189
190     $selectElement->check();
191   }
192
193   /**
194    * @Then /^I uncheck checkbox with id "([^"]*)"$/
195    * @param string $id
196    */
197   public function uncheckCheckbox($id)
198   {
199     $page          = $this->getSession()->getPage();
200     $selectElement = $page->find('xpath', '//input[@id = "' . $id . '"]');
201
202     $selectElement->uncheck();
203   }
204
205   /**
206    * @Then /^I select "([^"]*)" in "([^"]*)"$/
207    * @param string $label
208    * @param string $id
209    */
210   public function selectOption($label, $id)
211   {
212     $page          = $this->getSession()->getPage();
213     $selectElement = $page->find('xpath', '//select[@id = "' . $id . '"]');
214     $selectElement->selectOption($label);
215   }
216
217   /**
218    * @Then /^I should see text matching "([^"]*)" after a while$/
219    */
220   public function iShouldSeeTextAfterAWhile($text)
221   {
222     try {
223       $startTime = time();
224       do {
225         $content = $this->getSession()->getPage()->getText();
226         if (substr_count($content, $text) > 0) {
227           return true;
228         }
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),
232         $this->getSession()
233       );
234     } catch (StaleElementReference $e) {
235       return true;
236     }
237   }
238
239   /**
240    * @Then /^I click by selector "([^"]*)" via JavaScript$/
241    * @param string $selector
242    */
243   public function clickBySelector(string $selector)
244   {
245     $this->getSession()->executeScript("document.querySelector('" . $selector . "').click()");
246   }
247
248 }