Version 1
[yaffs-website] / web / core / modules / user / tests / src / Kernel / Views / HandlerFilterCurrentUserTest.php
1 <?php
2
3 namespace Drupal\Tests\user\Kernel\Views;
4
5 use Drupal\views\Views;
6 use Drupal\Core\Session\AnonymousUserSession;
7
8 /**
9  * Tests the current user filter handler.
10  *
11  * @group user
12  * @see \Drupal\user\Plugin\views\filter\Current
13  */
14 class HandlerFilterCurrentUserTest extends UserKernelTestBase {
15
16   /**
17    * Views used by this test.
18    *
19    * @var array
20    */
21   public static $testViews = ['test_filter_current_user'];
22
23   /**
24    * The current user.
25    *
26    * @var \Drupal\Core\Session\AccountProxy
27    */
28   protected $currentUser;
29
30   /**
31    * {@inheritdoc}
32    */
33   protected function setUp($import_test_views = TRUE) {
34     parent::setUp();
35     $this->currentUser = $this->container->get('current_user');
36     $this->setupPermissionTestData();
37   }
38
39   /**
40    * Tests the current user filter handler with anonymous user.
41    */
42   public function testFilterCurrentUserAsAnonymous() {
43     $column_map = ['uid' => 'uid'];
44     $this->currentUser->setAccount(new AnonymousUserSession());
45
46     $view = Views::getView('test_filter_current_user');
47     $view->initHandlers();
48     $view->filter['uid_current']->value = 0;
49     $this->executeView($view);
50     $expected[] = ['uid' => 1];
51     $expected[] = ['uid' => 2];
52     $expected[] = ['uid' => 3];
53     $expected[] = ['uid' => 4];
54     $this->assertIdenticalResultset($view, $expected, $column_map, 'Anonymous account can view all accounts when current filter is FALSE.');
55     $view->destroy();
56
57     $view = Views::getView('test_filter_current_user');
58     $view->initHandlers();
59     $view->filter['uid_current']->value = 1;
60     $this->executeView($view);
61     $expected = [];
62     $this->assertIdenticalResultset($view, $expected, $column_map, 'Anonymous account can view zero accounts when current filter is TRUE.');
63     $view->destroy();
64   }
65
66   /**
67    * Tests the current user filter handler with logged-in user.
68    */
69   public function testFilterCurrentUserAsUser() {
70     $column_map = ['uid' => 'uid'];
71     $user = reset($this->users);
72     $this->currentUser->setAccount($user);
73
74     $view = Views::getView('test_filter_current_user');
75     $view->initHandlers();
76     $view->filter['uid_current']->value = 0;
77     $this->executeView($view);
78     $expected = [];
79     $expected[] = ['uid' => 2];
80     $expected[] = ['uid' => 3];
81     $expected[] = ['uid' => 4];
82     $this->assertIdenticalResultset($view, $expected, $column_map, 'User can view all users except itself when current filter is FALSE.');
83     $view->destroy();
84
85     $view = Views::getView('test_filter_current_user');
86     $view->initHandlers();
87     $view->filter['uid_current']->value = 1;
88     $this->executeView($view);
89     $expected = [];
90     $expected[] = ['uid' => 1];
91     $this->assertIdenticalResultset($view, $expected, $column_map, 'User can only view itself when current filter is TRUE.');
92     $view->destroy();
93   }
94
95 }