Version 1
[yaffs-website] / web / core / modules / user / tests / src / Kernel / Views / UserKernelTestBase.php
1 <?php
2
3 namespace Drupal\Tests\user\Kernel\Views;
4
5 use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
6 use Drupal\views\Tests\ViewTestData;
7
8 /**
9  * Provides a common test base for user views tests.
10  */
11 abstract class UserKernelTestBase extends ViewsKernelTestBase {
12
13   /**
14    * Modules to enable.
15    *
16    * @var array
17    */
18   public static $modules = ['user_test_views', 'user', 'system', 'field'];
19
20   /**
21    * Users to use during this test.
22    *
23    * @var array
24    */
25   protected $users = [];
26
27   /**
28    * The entity storage for roles.
29    *
30    * @var \Drupal\user\RoleStorage
31    */
32   protected $roleStorage;
33
34   /**
35    * The entity storage for users.
36    *
37    * @var \Drupal\user\UserStorage
38    */
39   protected $userStorage;
40
41   protected function setUp($import_test_views = TRUE) {
42     parent::setUp();
43
44     ViewTestData::createTestViews(get_class($this), ['user_test_views']);
45
46     $this->installEntitySchema('user');
47
48     $entity_manager = $this->container->get('entity.manager');
49     $this->roleStorage = $entity_manager->getStorage('user_role');
50     $this->userStorage = $entity_manager->getStorage('user');
51   }
52
53   /**
54    * Set some test data for permission related tests.
55    */
56   protected function setupPermissionTestData() {
57     // Setup a role without any permission.
58     $this->roleStorage->create(['id' => 'authenticated'])
59       ->save();
60     $this->roleStorage->create(['id' => 'no_permission'])
61       ->save();
62     // Setup a role with just one permission.
63     $this->roleStorage->create(['id' => 'one_permission'])
64       ->save();
65     user_role_grant_permissions('one_permission', ['administer permissions']);
66     // Setup a role with multiple permissions.
67     $this->roleStorage->create(['id' => 'multiple_permissions'])
68       ->save();
69     user_role_grant_permissions('multiple_permissions', ['administer permissions', 'administer users', 'access user profiles']);
70
71     // Setup a user without an extra role.
72     $this->users[] = $account = $this->userStorage->create(['name' => $this->randomString()]);
73     $account->save();
74     // Setup a user with just the first role (so no permission beside the
75     // ones from the authenticated role).
76     $this->users[] = $account = $this->userStorage->create(['name' => 'first_role']);
77     $account->addRole('no_permission');
78     $account->save();
79     // Setup a user with just the second role (so one additional permission).
80     $this->users[] = $account = $this->userStorage->create(['name' => 'second_role']);
81     $account->addRole('one_permission');
82     $account->save();
83     // Setup a user with both the second and the third role.
84     $this->users[] = $account = $this->userStorage->create(['name' => 'second_third_role']);
85     $account->addRole('one_permission');
86     $account->addRole('multiple_permissions');
87     $account->save();
88   }
89
90 }