ef021b553e24eb7d72b0a64e7c321931fce4edb1
[yaffs-website] / web / modules / contrib / redirect / modules / redirect_404 / src / Tests / Redirect404LogSuppressorTest.php
1 <?php
2
3 namespace Drupal\redirect_404\Tests;
4
5 /**
6  * Tests suppressing 404 logs if the suppress_404 setting is enabled.
7  *
8  * @group redirect_404
9  */
10 class Redirect404LogSuppressorTest extends Redirect404TestBase {
11
12   /**
13    * Additional modules to enable.
14    *
15    * @var array
16    */
17   public static $modules = ['dblog'];
18
19   /**
20    * A user with some relevant administrative permissions.
21    *
22    * @var \Drupal\user\UserInterface
23    */
24   protected $adminUser;
25
26   /**
27    * A user without any permissions.
28    *
29    * @var \Drupal\user\UserInterface
30    */
31   protected $webUser;
32
33   /**
34    * {@inheritdoc}
35    */
36   public function setUp() {
37     parent::setUp();
38
39     // Create users with specific permissions.
40     $this->adminUser = $this->drupalCreateUser([
41       'administer redirect settings',
42       'administer redirects',
43     ]);
44     $this->webUser = $this->drupalCreateUser([]);
45   }
46
47   /**
48    * Tests the suppress_404 service.
49    */
50   public function testSuppress404Events() {
51     // Cause a page not found and an access denied event.
52     $this->drupalGet('page-not-found');
53     $this->assertResponse(404);
54     $this->drupalLogin($this->webUser);
55     $this->drupalGet('admin/reports/dblog');
56     $this->assertResponse(403);
57
58     // Assert the events are logged in the dblog reports.
59     $this->assertEqual(db_query("SELECT COUNT(*) FROM {watchdog} WHERE type = 'page not found'")->fetchField(), 1);
60     $this->assertEqual(db_query("SELECT COUNT(*) FROM {watchdog} WHERE type = 'access denied'")->fetchField(), 1);
61
62     // Login as admin and enable suppress_404 to avoid logging the 404 event.
63     $this->drupalLogin($this->adminUser);
64     $edit = ['suppress_404' => TRUE];
65     $this->drupalPostForm('admin/config/search/redirect/settings', $edit, 'Save configuration');
66
67     // Cause again a page not found and an access denied event.
68     $this->drupalGet('page-not-found');
69     $this->assertResponse(404);
70     $this->drupalLogin($this->webUser);
71     $this->drupalGet('admin/reports/dblog');
72     $this->assertResponse(403);
73
74     // Assert only the new access denied event is logged now.
75     $this->drupalLogin($this->adminUser);
76     $this->assertEqual(db_query("SELECT COUNT(*) FROM {watchdog} WHERE type = 'page not found'")->fetchField(), 1);
77     $this->assertEqual(db_query("SELECT COUNT(*) FROM {watchdog} WHERE type = 'access denied'")->fetchField(), 2);
78
79   }
80 }