Security update for Core, with self-updated composer
[yaffs-website] / web / modules / contrib / diff / tests / src / Kernel / DiffControllerTest.php
1 <?php
2
3 namespace Drupal\Tests\diff\Kernel;
4
5 use Drupal\Core\Url;
6 use Drupal\entity_test\Entity\EntityTestRev;
7 use Drupal\KernelTests\KernelTestBase;
8 use Drupal\user\Entity\Role;
9 use Drupal\user\Entity\User;
10 use Symfony\Component\HttpFoundation\Request;
11
12 /**
13  * Tests the diff controller.
14  *
15  * @group diff
16  */
17 class DiffControllerTest extends KernelTestBase {
18
19   /**
20    * {@inheritdoc}
21    */
22   public static $modules = [
23     'diff',
24     'entity_test',
25     'diff_test',
26     'system',
27     'user',
28   ];
29
30   /**
31    * {@inheritdoc}
32    */
33   protected function setUp() {
34     parent::setUp();
35
36     $this->installEntitySchema('entity_test_rev');
37     $this->installEntitySchema('user');
38     $this->installSchema('system', ['router', 'sequences']);
39     $this->installSchema('user', 'users_data');
40     \Drupal::service('router.builder')->rebuild();
41
42     $this->installConfig('diff');
43     $config = \Drupal::configFactory()->getEditable('diff.settings');
44     $config->set('entity.entity_test_rev.name', TRUE);
45     $config->save();
46   }
47
48   /**
49    * Tests the Controller.
50    */
51   public function testController() {
52     $entity = EntityTestRev::create([
53       'name' => 'test entity 1',
54       'type' => 'entity_test_rev',
55     ]);
56     $entity->save();
57     $vid1 = $entity->getRevisionId();
58
59     $entity->name->value = 'test entity 2';
60     $entity->setNewRevision(TRUE);
61     $entity->save();
62     $vi2 = $entity->getRevisionId();
63
64     /** @var \Symfony\Component\HttpKernel\HttpKernelInterface $http_kernel */
65     $http_kernel = \Drupal::service('http_kernel');
66     $request = Request::create(Url::fromRoute('entity.entity_test_rev.revisions_diff', [
67       'node' => $entity->id(),
68       'entity_test_rev' => $entity->id(),
69       'left_revision' => $vid1,
70       'right_revision' => $vi2,
71     ])->toString(TRUE)->getGeneratedUrl());
72
73     $response = $http_kernel->handle($request);
74     $this->assertEquals(403, $response->getStatusCode());
75
76     $role = Role::create([
77       'id' => 'test_role',
78     ]);
79     $role->grantPermission('administer entity_test content');
80     $role->save();
81     $account = User::create([
82       'name' => 'test user',
83       'roles' => $role->id(),
84     ]);
85     $account->save();
86
87     \Drupal::currentUser()->setAccount($account);
88     $response = $http_kernel->handle($request);
89     $this->assertEquals(200, $response->getStatusCode());
90
91     $output = $response->getContent();
92     $this->assertContains('<td class="diff-context diff-deletedline">test entity <span class="diffchange">1</span></td>', $output);
93     $this->assertContains('<td class="diff-context diff-addedline">test entity <span class="diffchange">2</span></td>', $output);
94   }
95
96 }