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\taxonomy\Entity\Vocabulary;
12 use Drupal\user\Entity\User;
13 use Drupal\permissions_by_term\Service\AccessCheck;
14 use Drupal\taxonomy\Entity\Term;
15 use Drupal\permissions_by_term\Service\AccessStorage;
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 $nidNoTerms;
47 public static $modules = ['taxonomy', 'node', 'user', 'text', 'field', 'system', 'permissions_by_term'];
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->installSchema('node', 'node_access');
71 $this->installSchema('permissions_by_term', 'permissions_by_term_user');
72 $this->installSchema('permissions_by_term', 'permissions_by_term_role');
73 $this->accessStorage = $this->container->get('permissions_by_term.access_storage');
74 $this->accessCheck = $this->container->get('permissions_by_term.access_check');
75 \Drupal::configFactory()->getEditable('taxonomy.settings')->set('maintain_index_table', TRUE)->save();
76 $this->createTestVocabulary();
77 $this->createPageNodeType();
78 $this->createCurrentUser();
81 protected function createTestVocabulary() {
88 protected function createPageNodeType() {
92 FieldStorageConfig::create([
93 'entity_type' => 'node',
94 'field_name' => 'field_tags',
95 'type' => 'entity_reference',
97 'target_type' => 'taxonomy_term',
99 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
102 FieldConfig::create([
103 'field_name' => 'field_tags',
104 'entity_type' => 'node',
109 protected function createCurrentUser() {
110 $testUser = User::create([
113 'mail' => 'foobar@example.com',
117 \Drupal::service('current_user')->setAccount($testUser);
123 protected function createRelationOneGrantedTerm() {
124 $term = Term::create([
129 $tids[] = $term->id();
131 $this->accessStorage->addTermPermissionsByUserIds([\Drupal::service('current_user')->id()], $term->id());
133 $term = Term::create([
138 $tids[] = $term->id();
140 $this->accessStorage->addTermPermissionsByUserIds([99], $term->id());
142 $node = Node::create([
144 'title' => 'test_title',
147 'target_id' => $tids[0]
150 'target_id' => $tids[1]
155 $this->setNidOneGrantedTerm($node->id());
161 protected function createRelationNoGrantedTerm() {
162 $term = Term::create([
167 $tids[] = $term->id();
169 $this->accessStorage->addTermPermissionsByUserIds([1], $term->id());
171 $node = Node::create([
173 'title' => 'test_title',
176 'target_id' => $tids[0]
181 $this->setNidNoGrantedTerm($node->id());
184 protected function createRelationAllGrantedTerms() {
185 $term = Term::create([
190 $tids[] = $term->id();
192 $this->accessStorage->addTermPermissionsByUserIds([\Drupal::service('current_user')->id()], $term->id());
194 $term = Term::create([
199 $tids[] = $term->id();
201 $this->accessStorage->addTermPermissionsByUserIds([\Drupal::service('current_user')->id()], $term->id());
203 $node = Node::create([
205 'title' => 'test_title',
208 'target_id' => $tids[0]
211 'target_id' => $tids[1]
216 $this->setNidAllGrantedTerms($node->id());
219 protected function createRelationNoTerms() {
220 $term = Term::create([
225 $tids[] = $term->id();
227 $this->accessStorage->addTermPermissionsByUserIds([\Drupal::service('current_user')->id()], $term->id());
229 $term = Term::create([
234 $tids[] = $term->id();
236 $this->accessStorage->addTermPermissionsByUserIds([\Drupal::service('current_user')->id()], $term->id());
238 $node = Node::create([
240 'title' => 'test_title',
243 'target_id' => $tids[0]
246 'target_id' => $tids[1]
251 $this->setNidNoTerms($node->id());
255 protected function getTaxonomyIndex() {
256 return \Drupal::database()->select('taxonomy_index', 'ti')
257 ->fields('ti', ['tid'])
265 protected function getNidOneGrantedTerm() {
266 return $this->nidOneGrantedTerm;
270 * @param int $nidOneGrantedTerm
272 protected function setNidOneGrantedTerm($nidOneGrantedTerm) {
273 $this->nidOneGrantedTerm = $nidOneGrantedTerm;
279 protected function getNidAllGrantedTerms() {
280 return $this->nidAllGrantedTerms;
284 * @param int $nidAllGrantedTerms
286 protected function setNidAllGrantedTerms($nidAllGrantedTerms) {
287 $this->nidAllGrantedTerms = $nidAllGrantedTerms;
293 protected function getNidNoGrantedTerm() {
294 return $this->nidNoGrantedTerm;
298 * @param int $nidNoGrantedTerm
300 protected function setNidNoGrantedTerm($nidNoGrantedTerm) {
301 $this->nidNoGrantedTerm = $nidNoGrantedTerm;
307 protected function getNidNoTerms() {
308 return $this->nidNoTerms;
312 * @param int $nidNoTerms
314 protected function setNidNoTerms($nidNoTerms) {
315 $this->nidNoTerms = $nidNoTerms;