X-Git-Url: http://aleph1.co.uk/gitweb/?a=blobdiff_plain;ds=sidebyside;f=web%2Fcore%2Fmodules%2Fviews%2Ftests%2Fsrc%2FKernel%2FHandler%2FFieldDropbuttonTest.php;fp=web%2Fcore%2Fmodules%2Fviews%2Ftests%2Fsrc%2FKernel%2FHandler%2FFieldDropbuttonTest.php;h=b789bd62ad3ce0c6fc31ac49b3c07d45d8d62db8;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/core/modules/views/tests/src/Kernel/Handler/FieldDropbuttonTest.php b/web/core/modules/views/tests/src/Kernel/Handler/FieldDropbuttonTest.php new file mode 100644 index 000000000..b789bd62a --- /dev/null +++ b/web/core/modules/views/tests/src/Kernel/Handler/FieldDropbuttonTest.php @@ -0,0 +1,167 @@ +installEntitySchema('node'); + $this->installEntitySchema('user'); + $this->installSchema('node', 'node_access'); + $this->installConfig('node'); + $this->installConfig('filter'); + + ViewTestData::createTestViews(get_class($this), ['views_test_config']); + // Create two node types. + $this->createContentType(['type' => 'foo']); + $this->createContentType(['type' => 'bar']); + + // Create user 1. + $admin = $this->createUser(); + + // And three nodes. + $this->node1 = $this->createNode([ + 'type' => 'bar', + 'title' => 'bazs', + 'status' => 1, + 'uid' => $admin->id(), + 'created' => REQUEST_TIME - 10, + ]); + $this->node2 = $this->createNode([ + 'type' => 'foo', + 'title' => 'foos', + 'status' => 1, + 'uid' => $admin->id(), + 'created' => REQUEST_TIME - 5, + ]); + $this->node3 = $this->createNode([ + 'type' => 'bar', + 'title' => 'bars', + 'status' => 1, + 'uid' => $admin->id(), + 'created' => REQUEST_TIME, + ]); + + // Now create a user with the ability to edit bar but not foo. + $this->testUser = $this->createUser([ + 'access content overview', + 'access content', + 'edit any bar content', + 'delete any bar content', + ]); + // And switch to that user. + $this->container->get('account_switcher')->switchTo($this->testUser); + } + + /** + * Tests that dropbutton markup doesn't leak between rows. + */ + public function testDropbuttonMarkupShouldNotLeakBetweenRows() { + $view = Views::getView('test_dropbutton'); + $view->setDisplay(); + $view->preExecute([]); + $view->execute(); + + $renderer = $this->container->get('renderer'); + $dropbutton_output = []; + + // Render each row and field in turn - the dropbutton plugin relies on + // output being set in previous versions. + foreach ($view->result as $index => $row) { + foreach (array_keys($view->field) as $field) { + $output = $renderer->executeInRenderContext(new RenderContext(), function () use ($view, $row, $field) { + return $view->field[$field]->advancedRender($row); + }); + if ($field === 'dropbutton') { + $dropbutton_output[] = $output; + } + } + } + + // The first row should contain edit links to node 3, as the user has + // access. + $this->assertContains($this->node3->toUrl('edit-form')->toString(), (string) $dropbutton_output[0]); + $this->assertContains($this->node3->toUrl('delete-form')->toString(), (string) $dropbutton_output[0]); + + // Second row should be not contain links to edit/delete any content as user + // has no edit/delete permissions. + // It most certainly should not contain links to node 3, as node 2 is the + // entity that forms this row. + $this->assertNotContains($this->node3->toUrl('edit-form')->toString(), (string) $dropbutton_output[1]); + $this->assertNotContains($this->node3->toUrl('delete-form')->toString(), (string) $dropbutton_output[1]); + $this->assertNotContains($this->node2->toUrl('edit-form')->toString(), (string) $dropbutton_output[1]); + $this->assertNotContains($this->node2->toUrl('delete-form')->toString(), (string) $dropbutton_output[1]); + + // Third row should contain links for node 1. + $this->assertContains($this->node1->toUrl('edit-form')->toString(), (string) $dropbutton_output[2]); + $this->assertContains($this->node1->toUrl('delete-form')->toString(), (string) $dropbutton_output[2]); + } + +}