Debian Codegraph @

extensions/Linter/tests/phpunit/DatabaseTest.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 namespace MediaWiki\Linter\Test;
22
23 use MediaWiki\Linter\Database;
24 use MediaWiki\Linter\LintError;
25 use MediaWikiIntegrationTestCase;
26
27 /**
28 * @group Database
29 * @covers \MediaWiki\Linter\Database
30 */
31 class DatabaseTest extends MediaWikiIntegrationTestCase {
32 private function getDatabase() {
33 return $this->getServiceContainer()->get( 'Linter.Database' );
34 }
35
36 public function testConstructor() {
37 $this->assertInstanceOf( Database::class, $this->getDatabase() );
38 }
39
40 private function getDummyLintErrors() {
41 return [
42 new LintError(
43 'fostered', [ 0, 10 ], []
44 ),
45 new LintError(
46 'obsolete-tag', [ 15, 20 ], [ 'name' => 'big' ]
47 ),
48 ];
49 }
50
51 private function assertSetForPageResult( $result, $deleted, $added ) {
52 $this->assertArrayHasKey( 'deleted', $result );
53 $this->assertEquals( $deleted, $result['deleted'] );
54 $this->assertArrayHasKey( 'added', $result );
55 $this->assertEquals( $added, $result['added'] );
56 }
57
58 private function assertLintErrorsEqual( $expected, $actual ) {
59 $expectedIds = array_map( static function ( LintError $error ) {
60 return $error->id();
61 }, $expected );
62 $actualIds = array_map( static function ( LintError $error ) {
63 return $error->id();
64 }, $actual );
65 $this->assertArrayEquals( $expectedIds, $actualIds );
66 }
67
68 private function createManyLintErrors(
69 $lintDb, int $pageId, int $namespaceId, $errorCount
70 ) {
71 $manyLintErrors = [];
72 for ( $i = 0; $i < $errorCount; $i++ ) {
73 $templateName = "Template:Echo";
74 $manyLintErrors[] = new LintError(
75 'obsolete-tag', [ 15, 20 + $i ], [ 'name' => 'big',
76 "templateInfo" => [ "name" => $templateName ] ]
77 );
78 }
79 $lintDb->setForPage( $pageId, $namespaceId, $manyLintErrors );
80 }
81
82 public function testSetForPage() {
83 $pageId = 5;
84 $namespaceId = 0;
85 $lintDb = $this->getDatabase();
86 $dummyErrors = $this->getDummyLintErrors();
87
88 $result = $lintDb->setForPage( $pageId, $namespaceId, $dummyErrors );
89 $this->assertSetForPageResult( $result, [], [ 'fostered' => 1, 'obsolete-tag' => 1 ] );
90 $this->assertLintErrorsEqual( $dummyErrors, $lintDb->getForPage( $pageId ) );
91
92 // Accurate low error count values should match for both methods
93 $resultTotals = $lintDb->getTotalsForPage( $pageId );
94 $resultEstimatedTotals = $lintDb->getTotals();
95 $this->assertEquals( $resultTotals, $resultEstimatedTotals );
96
97 // Should delete the second error
98 $result2 = $lintDb->setForPage( $pageId, $namespaceId, [ $dummyErrors[0] ] );
99 $this->assertSetForPageResult( $result2, [ 'obsolete-tag' => 1 ], [] );
100 $this->assertLintErrorsEqual( [ $dummyErrors[0] ], $lintDb->getForPage( $pageId ) );
101
102 // Accurate low error count values should match for both methods
103 $resultTotals = $lintDb->getTotalsForPage( $pageId );
104 $resultEstimatedTotals = $lintDb->getTotals();
105 $this->assertEquals( $resultTotals, $resultEstimatedTotals );
106
107 // Insert the second error, delete the first
108 $result3 = $lintDb->setForPage( $pageId, $namespaceId, [ $dummyErrors[1] ] );
109 $this->assertSetForPageResult( $result3, [ 'fostered' => 1 ], [ 'obsolete-tag' => 1 ] );
110 $this->assertLintErrorsEqual( [ $dummyErrors[1] ], $lintDb->getForPage( $pageId ) );
111
112 // Delete the second (only) error
113 $result4 = $lintDb->setForPage( $pageId, $namespaceId, [] );
114 $this->assertSetForPageResult( $result4, [ 'obsolete-tag' => 1 ], [] );
115 $this->assertLintErrorsEqual( [], $lintDb->getForPage( $pageId ) );
116
117 // Accurate low error count values should match for both methods
118 $resultTotals = $lintDb->getTotalsForPage( $pageId );
119 $resultEstimatedTotals = $lintDb->getTotals();
120 $this->assertEquals( $resultTotals, $resultEstimatedTotals );
121
122 // For error counts <= MAX_ACCURATE_COUNT, both error
123 // count methods should return the same count.
124 $this->createManyLintErrors( $lintDb, $pageId, $namespaceId, Database::MAX_ACCURATE_COUNT );
125 $resultTotals = $lintDb->getTotalsForPage( $pageId );
126 $resultEstimatedTotals = $lintDb->getTotals();
127 $this->assertEquals( $resultTotals, $resultEstimatedTotals );
128
129 // FIXME: These tests seem to be making false assumptions about
130 // `estimateRowCount()`. "EXPLAIN" is just going to give an estimate of
131 // the row count, it doesn't seem like there's any guarantee that it'll
132 // be higher or lower.
133 //
134 // // For error counts greater than MAX_ACCURATE_COUNT, both error
135 // // count methods should NOT return the same count in this test scenario
136 // // because previously added and deleted records will be included
137 // // in the estimated count which is normal.
138 // $this->createManyLintErrors( $lintDb, $pageId, $namespaceId, Database::MAX_ACCURATE_COUNT + 1 );
139 // $resultTotals = $lintDb->getTotalsForPage( $pageId );
140 // $resultEstimatedTotals = $lintDb->getTotals();
141 // $this->assertNotEquals( $resultTotals, $resultEstimatedTotals );
142 //
143 // // For error counts greatly above the MAX_ACCURATE_COUNT, the estimated
144 // // count method should return a greater count in this test scenario
145 // // because previously added and deleted records will be included
146 // // in the estimated count which is normal.
147 // $this->createManyLintErrors( $lintDb, $pageId, $namespaceId, Database::MAX_ACCURATE_COUNT * 10 );
148 // $resultTotals = $lintDb->getTotalsForPage( $pageId );
149 // $resultEstimatedTotals = $lintDb->getTotals();
150 // $this->assertGreaterThan( $resultTotals, $resultEstimatedTotals );
151 }
152
153 }