Security update for permissions_by_term
[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   public function __construct() {
18     $driver = new DrupalDriver(DRUPAL_ROOT, '');
19     $driver->setCoreFromVersion();
20
21     // Bootstrap Drupal.
22     $driver->bootstrap();
23   }
24
25   /**
26    * @AfterSuite
27    */
28   public static function cleanDB() {
29     $module_path = \Drupal::service('module_handler')->getModule('permissions_by_term')->getPath();
30
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);
35   }
36
37   /**
38    * Creates one or more terms on an existing vocabulary.
39    *
40    * Provide term data in the following format:
41    *
42    * | name  | parent | description | weight | taxonomy_field_image | access_user | access_role |
43    * | Snook | Fish   | Marine fish | 10     | snook-123.jpg        | Bob         | editor      |
44    * | ...   | ...    | ...         | ...    | ...                  | ...         | ...         |
45    *
46    * Only the 'name' field is required.
47    *
48    * @Given restricted :vocabulary terms:
49    */
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);
55
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);
61         }
62       }
63
64       if (!empty($termsHash['access_role'])) {
65         $rolesIds = explode(', ', $termsHash['access_role']);
66         $accessStorage->addTermPermissionsByRoleIds($rolesIds, $term->tid);
67       }
68     }
69   }
70
71   /**
72    * @Given /^I create vocabulary with name "([^"]*)" and vid "([^"]*)"$/
73    */
74   public function createVocabulary($name, $vid) {
75     $vocabulary = \Drupal::entityQuery('taxonomy_vocabulary')
76       ->condition('vid', $vid)
77       ->execute();
78
79     if (empty($vocabulary)) {
80       $vocabulary = Vocabulary::create([
81         'name' => $name,
82         'vid' => $vid,
83       ]);
84       $vocabulary->save();
85     }
86   }
87
88   /**
89    * @Then I open open Permissions By Term advanced info
90    */
91   public function iOpenOpenPermissionsByTermAdvancedInfo()
92   {
93     $this->getSession()->evaluateScript("jQuery('#edit-permissions-by-term-info').attr('open', true);");
94   }
95
96   /**
97    * @Given /^I create (\d+) nodes of type "([^"]*)"$/
98    */
99   public function iCreateNodesOfType($number, $type)
100   {
101     for ($i = 0; $i <= $number; $i++) {
102       $node = new \stdClass();
103       $node->type = $type;
104       $node->title = $this->createRandomString();
105       $node->body = $this->createRandomString();
106       $this->nodeCreate($node);
107     }
108   }
109
110   private function createRandomString($length = 10) {
111     return substr(str_shuffle(str_repeat("0123456789abcdefghijklmnopqrstuvwxyz", $length)), 0, $length);
112   }
113
114   /**
115    * @Given Node access records are rebuild.
116    */
117   public function nodeAccessRecordsAreRebuild()
118   {
119     node_access_rebuild();
120   }
121
122   /**
123    * @Then /^wait (\d+) seconds$/
124    */
125   public function waitSeconds($secondsNumber)
126   {
127     $this->getSession()->wait($secondsNumber * 1000);
128   }
129
130   /**
131    * @Then /^I select index (\d+) in dropdown named "([^"]*)"$/
132    */
133   public function selectIndexInDropdown($index, $name)
134   {
135     $this->getSession()->evaluateScript('document.getElementsByName("' . $name . '")[0].selectedIndex = ' . $index . ';');
136   }
137
138   /**
139    * @Then /^I open node edit form by node title "([^"]*)"$/
140    * @param string $title
141    */
142   public function openNodeEditFormByTitle($title)
143   {
144     $query = \Drupal::service('database')->select('node_field_data', 'nfd')
145       ->fields('nfd', ['nid'])
146       ->condition('nfd.title', $title);
147
148     $this->visitPath('/node/' . $query->execute()->fetchField() . '/edit');
149   }
150
151   /**
152    * @Then /^I open node view by node title "([^"]*)"$/
153    * @param string $title
154    */
155   public function openNodeViewByTitle($title)
156   {
157     $query = \Drupal::service('database')->select('node_field_data', 'nfd')
158       ->fields('nfd', ['nid'])
159       ->condition('nfd.title', $title);
160
161     $this->visitPath('/node/' . $query->execute()->fetchField());
162   }
163
164   /**
165    * @Then /^I scroll to element with id "([^"]*)"$/
166    * @param string $id
167    */
168   public function iScrollToElementWithId($id)
169   {
170     $this->getSession()->executeScript(
171       "
172                 var element = document.getElementById('" . $id . "');
173                 element.scrollIntoView( true );
174             "
175     );
176   }
177
178   /**
179    * @Then /^I check checkbox with id "([^"]*)" by JavaScript$/
180    * @param string $id
181    */
182   public function checkCheckboxWithJS($id)
183   {
184     $this->getSession()->executeScript(
185       "
186                 document.getElementById('" . $id . "').checked = true;
187             "
188     );
189   }
190
191   /**
192    * @Then /^I check checkbox with id "([^"]*)"$/
193    * @param string $id
194    */
195   public function checkCheckbox($id)
196   {
197     $page          = $this->getSession()->getPage();
198     $selectElement = $page->find('xpath', '//input[@id = "' . $id . '"]');
199
200     $selectElement->check();
201   }
202
203   /**
204    * @Then /^I uncheck checkbox with id "([^"]*)"$/
205    * @param string $id
206    */
207   public function uncheckCheckbox($id)
208   {
209     $page          = $this->getSession()->getPage();
210     $selectElement = $page->find('xpath', '//input[@id = "' . $id . '"]');
211
212     $selectElement->uncheck();
213   }
214
215   /**
216    * @Then /^I select "([^"]*)" in "([^"]*)"$/
217    * @param string $label
218    * @param string $id
219    */
220   public function selectOption($label, $id)
221   {
222     $page          = $this->getSession()->getPage();
223     $selectElement = $page->find('xpath', '//select[@id = "' . $id . '"]');
224     $selectElement->selectOption($label);
225   }
226
227 }