5461f683118d3b0ebe09ca178fa6cb75c8cb7262
[yaffs-website] / DiffViewsTest.php
1 <?php
2
3 namespace Drupal\diff\Tests;
4
5 use Drupal\Core\Url;
6 use Drupal\node\Entity\Node;
7 use Drupal\node\Entity\NodeType;
8 use Drupal\views\Tests\ViewTestBase;
9
10 /**
11  * Tests the diff views integration.
12  *
13  * Loads optional config of views.
14  *
15  * @group diff
16  */
17 class DiffViewsTest extends ViewTestBase {
18
19   /**
20    * {@inheritdoc}
21    */
22   public static $modules = ['node', 'diff', 'user', 'views', 'diff_test'];
23
24   /**
25    * Tests the behavior of a view that uses the diff_from and diff_to fields.
26    */
27   public function testDiffView() {
28     // Make sure HTML Diff is disabled.
29     $config = \Drupal::configFactory()->getEditable('diff.settings');
30     $config->set('general_settings.layout_plugins.visual_inline.enabled', FALSE)->save();
31
32     $node_type = NodeType::create([
33       'type' => 'article',
34       'label' => 'Article',
35     ]);
36     $node_type->save();
37     $node = Node::create([
38       'type' => 'article',
39       'title' => 'Test article: giraffe',
40     ]);
41     $node->save();
42     $revision1 = $node->getRevisionId();
43
44     $node->setNewRevision(TRUE);
45     $node->setTitle('Test article: llama');
46     $node->save();
47     $revision2 = $node->getRevisionId();
48
49     $this->drupalGet("node/{$node->id()}/diff-views");
50     $this->assertResponse(403);
51
52     $user = $this->createUser(['view all revisions']);
53     $this->drupalLogin($user);
54
55     $this->drupalGet("node/{$node->id()}/diff-views");
56     $this->assertResponse(200);
57
58     $from_first = (string) $this->cssSelect('#edit-diff-from--3')[0]->attributes()['value'];
59     $to_second = (string) $this->cssSelect('#edit-diff-to--2')[0]->attributes()['value'];
60
61     $edit = [
62       'diff_from' => $from_first,
63       'diff_to' => $to_second,
64     ];
65     $this->drupalPostForm(NULL, $edit, t('Compare'));
66     $expected_url = Url::fromRoute(
67       'diff.revisions_diff',
68       // Route parameters.
69       [
70         'node' => $node->id(),
71         'left_revision' => $revision1,
72         'right_revision' => $revision2,
73         'filter' => 'split_fields',
74       ],
75       // Additional route options.
76       [
77         'query' => [
78           'destination' => Url::fromUri("internal:/node/{$node->id()}/diff-views")->toString(),
79         ],
80       ]
81     );
82     $this->assertUrl($expected_url);
83     $this->assertRaw('<td class="diff-context diff-deletedline">Test article: <span class="diffchange">giraffe</span></td>');
84     $this->assertRaw('<td class="diff-context diff-addedline">Test article: <span class="diffchange">llama</span></td>');
85   }
86
87 }