3 namespace Drupal\Tests\permissions_by_term\Kernel;
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Entity\FieldStorageConfig;
8 use Drupal\KernelTests\KernelTestBase;
9 use Drupal\node\Entity\Node;
10 use Drupal\node\Entity\NodeType;
11 use Drupal\permissions_by_term\Service\AccessCheck;
12 use Drupal\permissions_by_term\Service\AccessStorage;
13 use Drupal\taxonomy\Entity\Term;
14 use Drupal\taxonomy\Entity\Vocabulary;
15 use Drupal\user\Entity\User;
18 * Class PBTKernelTestBase
20 * @package Drupal\Tests\permissions_by_term\Kernel
22 abstract class PBTKernelTestBase extends KernelTestBase {
27 protected $nidOneGrantedTerm;
32 protected $nidNoGrantedTerm;
37 protected $nidAllGrantedTerms;
42 protected $nidNoRestriction;
47 public static $modules = ['taxonomy', 'node', 'user', 'text', 'field', 'system', 'permissions_by_term', 'language'];
52 protected $accessStorage;
57 protected $accessCheck;
62 protected function setUp() {
65 $this->installEntitySchema('user');
66 $this->installSchema('system', ['key_value_expire', 'sequences']);
67 $this->installEntitySchema('node');
68 $this->installEntitySchema('taxonomy_term');
69 $this->installConfig(['permissions_by_term']);
70 $this->installConfig(['language']);
71 $this->installSchema('node', 'node_access');
72 $this->installSchema('permissions_by_term', 'permissions_by_term_user');
73 $this->installSchema('permissions_by_term', 'permissions_by_term_role');
74 $this->accessStorage = $this->container->get('permissions_by_term.access_storage');
75 $this->accessCheck = $this->container->get('permissions_by_term.access_check');
76 \Drupal::configFactory()->getEditable('taxonomy.settings')->set('maintain_index_table', TRUE)->save();
77 $this->createTestVocabularies();
78 $this->createPageNodeType();
79 $this->createCurrentUser();
82 protected function createTestVocabularies() {
94 protected function createPageNodeType() {
98 FieldStorageConfig::create([
99 'entity_type' => 'node',
100 'field_name' => 'field_tags',
101 'type' => 'entity_reference',
103 'target_type' => 'taxonomy_term',
105 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
108 FieldStorageConfig::create([
109 'entity_type' => 'node',
110 'field_name' => 'field_tags2',
111 'type' => 'entity_reference',
113 'target_type' => 'taxonomy_term',
115 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
118 FieldConfig::create([
119 'field_name' => 'field_tags',
120 'entity_type' => 'node',
124 FieldConfig::create([
125 'field_name' => 'field_tags2',
126 'entity_type' => 'node',
131 protected function createCurrentUser() {
132 $testUser = User::create([
135 'mail' => 'foobar@example.com',
139 \Drupal::service('current_user')->setAccount($testUser);
145 protected function createRelationOneGrantedTerm() {
146 $term = Term::create([
151 $tids[] = $term->id();
153 $this->accessStorage->addTermPermissionsByUserIds([\Drupal::service('current_user')->id()], $term->id());
155 $term = Term::create([
160 $tids[] = $term->id();
162 $term = Term::create([
167 $tids[] = $term->id();
169 $this->accessStorage->addTermPermissionsByUserIds([99], $term->id());
171 $node = Node::create([
173 'title' => 'test_title',
176 'target_id' => $tids[0]
179 'target_id' => $tids[1]
184 'target_id' => $tids[2]
189 $this->setNidOneGrantedTerm($node->id());
195 protected function createRelationNoGrantedTerm() {
196 $term = Term::create([
201 $tids[] = $term->id();
203 $this->accessStorage->addTermPermissionsByUserIds([1], $term->id());
205 $node = Node::create([
207 'title' => 'test_title',
210 'target_id' => $tids[0]
215 $this->setNidNoGrantedTerm($node->id());
218 protected function createRelationAllGrantedTerms() {
219 $term = Term::create([
224 $tids[] = $term->id();
226 $this->accessStorage->addTermPermissionsByUserIds([\Drupal::service('current_user')->id()], $term->id());
228 $term = Term::create([
233 $tids[] = $term->id();
235 $this->accessStorage->addTermPermissionsByUserIds([\Drupal::service('current_user')->id()], $term->id());
237 $node = Node::create([
239 'title' => 'test_title',
242 'target_id' => $tids[0]
245 'target_id' => $tids[1]
250 $this->setNidAllGrantedTerms($node->id());
253 protected function createRelationWithoutRestriction() {
254 $term = Term::create([
259 $tids[] = $term->id();
261 $term = Term::create([
266 $tids[] = $term->id();
268 $node = Node::create([
270 'title' => 'test_title',
273 'target_id' => $tids[0]
276 'target_id' => $tids[1]
281 $this->setNidNoRestriction($node->id());
285 protected function getTaxonomyIndex() {
286 return \Drupal::database()->select('taxonomy_index', 'ti')
287 ->fields('ti', ['tid'])
295 protected function getNidOneGrantedTerm() {
296 return $this->nidOneGrantedTerm;
300 * @param int $nidOneGrantedTerm
302 protected function setNidOneGrantedTerm($nidOneGrantedTerm) {
303 $this->nidOneGrantedTerm = $nidOneGrantedTerm;
309 protected function getNidAllGrantedTerms() {
310 return $this->nidAllGrantedTerms;
314 * @param int $nidAllGrantedTerms
316 protected function setNidAllGrantedTerms($nidAllGrantedTerms) {
317 $this->nidAllGrantedTerms = $nidAllGrantedTerms;
323 protected function getNidNoGrantedTerm() {
324 return $this->nidNoGrantedTerm;
328 * @param int $nidNoGrantedTerm
330 protected function setNidNoGrantedTerm($nidNoGrantedTerm) {
331 $this->nidNoGrantedTerm = $nidNoGrantedTerm;
337 protected function getNidNoRestriction() {
338 return $this->nidNoRestriction;
342 * @param int $nidNoRestiction
344 protected function setNidNoRestriction($nidNoRestiction) {
345 $this->nidNoRestriction = $nidNoRestiction;