Version 1
[yaffs-website] / web / core / modules / node / tests / src / Functional / Views / RevisionLinkTest.php
1 <?php
2
3 namespace Drupal\Tests\node\Functional\Views;
4
5 /**
6  * Tests the different revision link handlers.
7  *
8  * @group node
9  *
10  * @see \Drupal\node\Plugin\views\field\RevisionLink
11  * @see \Drupal\node\Plugin\views\field\RevisionLinkDelete
12  * @see \Drupal\node\Plugin\views\field\RevisionLinkRevert
13  */
14 class RevisionLinkTest extends NodeTestBase {
15
16   /**
17    * Views used by this test.
18    *
19    * @var array
20    */
21   public static $testViews = ['test_node_revision_links'];
22
23   /**
24    * Tests revision links.
25    */
26   public function testRevisionLinks() {
27     // Create one user which can view/revert and delete and one which can only
28     // do one of them.
29     $this->drupalCreateContentType(['name' => 'page', 'type' => 'page']);
30     $account = $this->drupalCreateUser(['revert all revisions', 'view all revisions', 'delete all revisions', 'edit any page content', 'delete any page content']);
31     $this->drupalLogin($account);
32     // Create two nodes, one without an additional revision and one with a
33     // revision.
34     $nodes = [
35       $this->drupalCreateNode(),
36       $this->drupalCreateNode(),
37     ];
38
39     $first_revision = $nodes[1]->getRevisionId();
40     // Create revision of the node.
41     $nodes[1]->setNewRevision();
42     $nodes[1]->save();
43     $second_revision = $nodes[1]->getRevisionId();
44
45     $this->drupalGet('test-node-revision-links');
46     $this->assertResponse(200, 'Test view can be accessed in the path expected');
47     // The first node revision should link to the node directly as you get an
48     // access denied if you link to the revision.
49     $url = $nodes[0]->urlInfo()->toString();
50     $this->assertLinkByHref($url);
51     $this->assertNoLinkByHref($url . '/revisions/' . $nodes[0]->getRevisionId() . '/view');
52     $this->assertNoLinkByHref($url . '/revisions/' . $nodes[0]->getRevisionId() . '/delete');
53     $this->assertNoLinkByHref($url . '/revisions/' . $nodes[0]->getRevisionId() . '/revert');
54
55     // For the second node the current revision got set to the last revision, so
56     // the first one should also link to the node page itself.
57     $url = $nodes[1]->urlInfo()->toString();
58     $this->assertLinkByHref($url);
59     $this->assertLinkByHref($url . '/revisions/' . $first_revision . '/view');
60     $this->assertLinkByHref($url . '/revisions/' . $first_revision . '/delete');
61     $this->assertLinkByHref($url . '/revisions/' . $first_revision . '/revert');
62     $this->assertNoLinkByHref($url . '/revisions/' . $second_revision . '/view');
63     $this->assertNoLinkByHref($url . '/revisions/' . $second_revision . '/delete');
64     $this->assertNoLinkByHref($url . '/revisions/' . $second_revision . '/revert');
65
66     $accounts = [
67       'view' => $this->drupalCreateUser(['view all revisions']),
68       'revert' => $this->drupalCreateUser(['revert all revisions', 'edit any page content']),
69       'delete' => $this->drupalCreateUser(['delete all revisions', 'delete any page content']),
70     ];
71
72     $url = $nodes[1]->urlInfo()->toString();
73     // Render the view with users which can only delete/revert revisions.
74     foreach ($accounts as $allowed_operation => $account) {
75       $this->drupalLogin($account);
76       $this->drupalGet('test-node-revision-links');
77       // Check expected links.
78       foreach (['revert', 'delete'] as $operation) {
79         if ($operation == $allowed_operation) {
80           $this->assertLinkByHref($url . '/revisions/' . $first_revision . '/' . $operation);
81         }
82         else {
83           $this->assertNoLinkByHref($url . '/revisions/' . $first_revision . '/' . $operation);
84         }
85       }
86     }
87   }
88
89 }