41aa6ec53a7c5b25dff9c55dd3c696d6a940bb3d
[yaffs-website] / web / core / modules / views / tests / src / Functional / Plugin / AccessTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Functional\Plugin;
4
5 use Drupal\Tests\views\Functional\ViewTestBase;
6 use Drupal\views\Tests\ViewTestData;
7 use Drupal\views\Views;
8
9 /**
10  * Tests pluggable access for views.
11  *
12  * @group views
13  * @todo It probably make sense to split the test up by one for role/perm/none
14  *   and the two generic ones.
15  */
16 class AccessTest extends ViewTestBase {
17
18   /**
19    * Views used by this test.
20    *
21    * @var array
22    */
23   public static $testViews = ['test_access_none', 'test_access_static', 'test_access_dynamic'];
24
25   /**
26    * Modules to enable.
27    *
28    * @return array
29    */
30   public static $modules = ['node'];
31
32   /**
33    * Web user for testing.
34    *
35    * @var \Drupal\user\UserInterface
36    */
37   protected $webUser;
38
39   /**
40    * Normal user for testing.
41    *
42    * @var \Drupal\user\UserInterface
43    */
44   protected $normalUser;
45
46   protected function setUp($import_test_views = TRUE) {
47     parent::setUp($import_test_views);
48
49     $this->enableViewsTestModule();
50
51     ViewTestData::createTestViews(get_class($this), ['views_test_data']);
52
53     $this->webUser = $this->drupalCreateUser();
54
55     $normal_role = $this->drupalCreateRole([]);
56     $this->normalUser = $this->drupalCreateUser(['views_test_data test permission']);
57     $this->normalUser->addRole($normal_role);
58     // @todo when all the plugin information is cached make a reset function and
59     // call it here.
60   }
61
62   /**
63    * Tests none access plugin.
64    */
65   public function testAccessNone() {
66     $view = Views::getView('test_access_none');
67     $view->setDisplay();
68
69     $this->assertTrue($view->display_handler->access($this->webUser));
70     $this->assertTrue($view->display_handler->access($this->normalUser));
71   }
72
73   /**
74    * @todo Test abstract access plugin.
75    */
76
77   /**
78    * Tests static access check.
79    *
80    * @see \Drupal\views_test\Plugin\views\access\StaticTest
81    */
82   public function testStaticAccessPlugin() {
83     $view = Views::getView('test_access_static');
84     $view->setDisplay();
85
86     $access_plugin = $view->display_handler->getPlugin('access');
87
88     $this->assertFalse($access_plugin->access($this->normalUser));
89     $this->drupalGet('test_access_static');
90     $this->assertResponse(403);
91
92     $display = &$view->storage->getDisplay('default');
93     $display['display_options']['access']['options']['access'] = TRUE;
94     $access_plugin->options['access'] = TRUE;
95     $view->save();
96     // Saving a view will cause the router to be rebuilt when the kernel
97     // termination event fires. Simulate that here.
98     $this->container->get('router.builder')->rebuildIfNeeded();
99
100     $this->assertTrue($access_plugin->access($this->normalUser));
101
102     $this->drupalGet('test_access_static');
103     $this->assertResponse(200);
104   }
105
106 }