Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / modules / contrib / redirect / modules / redirect_404 / tests / src / Unit / SqlRedirectNotFoundStorageTest.php
1 <?php
2
3 namespace Drupal\Tests\redirect_404\Unit;
4
5 use Drupal\Core\Database\Connection;
6 use Drupal\Core\Language\LanguageInterface;
7 use Drupal\redirect_404\SqlRedirectNotFoundStorage;
8 use Drupal\Tests\UnitTestCase;
9
10 /**
11  * Tests that overly long paths aren't logged.
12  *
13  * @group redirect_404
14  */
15 class SqlRedirectNotFoundStorageTest extends UnitTestCase {
16
17   /**
18    * Mock database connection.
19    *
20    * @var \Drupal\Core\Database\Connection|\PHPUnit_Framework_MockObject_MockObject
21    */
22   protected $database;
23
24   /**
25    * Mock config factory.
26    *
27    * @var \Drupal\Core\Config\ConfigFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
28    */
29   protected $configFactory;
30
31   /**
32    * {@inheritdoc}
33    */
34   protected function setUp() {
35     parent::setUp();
36     $this->database = $this->getMockBuilder(Connection::class)
37       ->disableOriginalConstructor()
38       ->getMock();
39   }
40
41   /**
42    * Tests that long paths aren't stored in the database.
43    */
44   public function testLongPath() {
45     $this->database->expects($this->never())
46       ->method('merge');
47     $storage = new SqlRedirectNotFoundStorage($this->database, $this->getConfigFactoryStub());
48     $storage->logRequest($this->randomMachineName(SqlRedirectNotFoundStorage::MAX_PATH_LENGTH + 1), LanguageInterface::LANGCODE_DEFAULT);
49   }
50
51   /**
52    * Tests that invalid UTF-8 paths are not stored in the database.
53    */
54   public function testInvalidUtf8Path() {
55     $this->database->expects($this->never())
56       ->method('merge');
57     $storage = new SqlRedirectNotFoundStorage($this->database, $this->getConfigFactoryStub());
58     $storage->logRequest("Caf\xc3", LanguageInterface::LANGCODE_DEFAULT);
59   }
60
61   /**
62    * Tests that all logs are kept if row limit config is "All".
63    */
64   public function testPurgeOldRequests() {
65     $this->configFactory = $this->getConfigFactoryStub(
66       [
67         'redirect_404.settings' => [
68           'row_limit' => 0,
69         ],
70       ]
71     );
72     $storage = new SqlRedirectNotFoundStorage($this->database, $this->configFactory);
73     $storage->purgeOldRequests();
74     $this->database->expects($this->never())
75       ->method('select');
76   }
77
78 }