Merge "Corrected grammatical error."
[lhc/web/wiklou.git] / tests / phpunit / includes / PagePropsTest.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4
5 /**
6 * @covers PageProps
7 *
8 * @group Database
9 * ^--- tell jenkins this test needs the database
10 *
11 * @group medium
12 * ^--- tell phpunit that these test cases may take longer than 2 seconds.
13 */
14 class PagePropsTest extends MediaWikiLangTestCase {
15
16 /**
17 * @var Title $title1
18 */
19 private $title1;
20
21 /**
22 * @var Title $title2
23 */
24 private $title2;
25
26 /**
27 * @var array $the_properties
28 */
29 private $the_properties;
30
31 protected function setUp() {
32 parent::setUp();
33
34 $this->setMwGlobals( [
35 'wgExtraNamespaces' => [
36 12312 => 'Dummy',
37 12313 => 'Dummy_talk',
38 ],
39 'wgNamespaceContentModels' => [ 12312 => 'DUMMY' ],
40 ] );
41
42 $this->mergeMwGlobalArrayValue(
43 'wgContentHandlers',
44 [ 'DUMMY' => 'DummyContentHandlerForTesting' ]
45 );
46
47 if ( !$this->the_properties ) {
48 $this->the_properties = [
49 "property1" => "value1",
50 "property2" => "value2",
51 "property3" => "value3",
52 "property4" => "value4"
53 ];
54 }
55
56 if ( !$this->title1 ) {
57 $page = $this->createPage(
58 'PagePropsTest_page_1',
59 "just a dummy page",
60 CONTENT_MODEL_WIKITEXT
61 );
62 $this->title1 = $page->getTitle();
63 $page1ID = $this->title1->getArticleID();
64 $this->setProperties( $page1ID, $this->the_properties );
65 }
66
67 if ( !$this->title2 ) {
68 $page = $this->createPage(
69 'PagePropsTest_page_2',
70 "just a dummy page",
71 CONTENT_MODEL_WIKITEXT
72 );
73 $this->title2 = $page->getTitle();
74 $page2ID = $this->title2->getArticleID();
75 $this->setProperties( $page2ID, $this->the_properties );
76 }
77 }
78
79 /**
80 * Test getting a single property from a single page. The property was
81 * set in setUp().
82 */
83 public function testGetSingleProperty() {
84 $pageProps = PageProps::getInstance();
85 $page1ID = $this->title1->getArticleID();
86 $result = $pageProps->getProperties( $this->title1, "property1" );
87 $this->assertArrayHasKey( $page1ID, $result, "Found property" );
88 $this->assertEquals( $result[$page1ID], "value1", "Get property" );
89 }
90
91 /**
92 * Test getting a single property from multiple pages. The property was
93 * set in setUp().
94 */
95 public function testGetSinglePropertyMultiplePages() {
96 $pageProps = PageProps::getInstance();
97 $page1ID = $this->title1->getArticleID();
98 $page2ID = $this->title2->getArticleID();
99 $titles = [
100 $this->title1,
101 $this->title2
102 ];
103 $result = $pageProps->getProperties( $titles, "property1" );
104 $this->assertArrayHasKey( $page1ID, $result, "Found page 1 property" );
105 $this->assertArrayHasKey( $page2ID, $result, "Found page 2 property" );
106 $this->assertEquals( $result[$page1ID], "value1", "Get property page 1" );
107 $this->assertEquals( $result[$page2ID], "value1", "Get property page 2" );
108 }
109
110 /**
111 * Test getting multiple properties from multiple pages. The properties
112 * were set in setUp().
113 */
114 public function testGetMultiplePropertiesMultiplePages() {
115 $pageProps = PageProps::getInstance();
116 $page1ID = $this->title1->getArticleID();
117 $page2ID = $this->title2->getArticleID();
118 $titles = [
119 $this->title1,
120 $this->title2
121 ];
122 $properties = [
123 "property1",
124 "property2"
125 ];
126 $result = $pageProps->getProperties( $titles, $properties );
127 $this->assertArrayHasKey( $page1ID, $result, "Found page 1 property" );
128 $this->assertArrayHasKey( "property1", $result[$page1ID], "Found page 1 property 1" );
129 $this->assertArrayHasKey( "property2", $result[$page1ID], "Found page 1 property 2" );
130 $this->assertArrayHasKey( $page2ID, $result, "Found page 2 property" );
131 $this->assertArrayHasKey( "property1", $result[$page2ID], "Found page 2 property 1" );
132 $this->assertArrayHasKey( "property2", $result[$page2ID], "Found page 2 property 2" );
133 $this->assertEquals( $result[$page1ID]["property1"], "value1", "Get page 1 property 1" );
134 $this->assertEquals( $result[$page1ID]["property2"], "value2", "Get page 1 property 2" );
135 $this->assertEquals( $result[$page2ID]["property1"], "value1", "Get page 2 property 1" );
136 $this->assertEquals( $result[$page2ID]["property2"], "value2", "Get page 2 property 2" );
137 }
138
139 /**
140 * Test getting all properties from a single page. The properties were
141 * set in setUp(). The properties retrieved from the page may include
142 * additional properties not set in the test case that are added by
143 * other extensions. Therefore, rather than checking to see if the
144 * properties that were set in the test case exactly match the
145 * retrieved properties, we need to check to see if they are a
146 * subset of the retrieved properties. Since this version of PHPUnit
147 * does not yet include assertArraySubset(), we needed to code the
148 * equivalent functionality.
149 */
150 public function testGetAllProperties() {
151 $pageProps = PageProps::getInstance();
152 $page1ID = $this->title1->getArticleID();
153 $result = $pageProps->getAllProperties( $this->title1 );
154 $this->assertArrayHasKey( $page1ID, $result, "Found properties" );
155 $properties = $result[$page1ID];
156 $patched = array_replace_recursive( $properties, $this->the_properties );
157 $this->assertEquals( $patched, $properties, "Get all properties" );
158 }
159
160 /**
161 * Test getting all properties from multiple pages. The properties were
162 * set in setUp(). See getAllProperties() above for more information.
163 */
164 public function testGetAllPropertiesMultiplePages() {
165 $pageProps = PageProps::getInstance();
166 $page1ID = $this->title1->getArticleID();
167 $page2ID = $this->title2->getArticleID();
168 $titles = [
169 $this->title1,
170 $this->title2
171 ];
172 $result = $pageProps->getAllProperties( $titles );
173 $this->assertArrayHasKey( $page1ID, $result, "Found page 1 properties" );
174 $this->assertArrayHasKey( $page2ID, $result, "Found page 2 properties" );
175 $properties1 = $result[$page1ID];
176 $patched = array_replace_recursive( $properties1, $this->the_properties );
177 $this->assertEquals( $patched, $properties1, "Get all properties page 1" );
178 $properties2 = $result[$page2ID];
179 $patched = array_replace_recursive( $properties2, $this->the_properties );
180 $this->assertEquals( $patched, $properties2, "Get all properties page 2" );
181 }
182
183 /**
184 * Test caching when retrieving single properties by getting a property,
185 * saving a new value for the property, then getting the property
186 * again. The cached value for the property rather than the new value
187 * of the property should be returned.
188 */
189 public function testSingleCache() {
190 $pageProps = PageProps::getInstance();
191 $page1ID = $this->title1->getArticleID();
192 $value1 = $pageProps->getProperties( $this->title1, "property1" );
193 $this->setProperty( $page1ID, "property1", "another value" );
194 $value2 = $pageProps->getProperties( $this->title1, "property1" );
195 $this->assertEquals( $value1, $value2, "Single cache" );
196 }
197
198 /**
199 * Test caching when retrieving all properties by getting all
200 * properties, saving a new value for a property, then getting all
201 * properties again. The cached value for the properties rather than the
202 * new value of the properties should be returned.
203 */
204 public function testMultiCache() {
205 $pageProps = PageProps::getInstance();
206 $page1ID = $this->title1->getArticleID();
207 $properties1 = $pageProps->getAllProperties( $this->title1 );
208 $this->setProperty( $page1ID, "property1", "another value" );
209 $properties2 = $pageProps->getAllProperties( $this->title1 );
210 $this->assertEquals( $properties1, $properties2, "Multi Cache" );
211 }
212
213 /**
214 * Test that getting all properties clears the single properties
215 * that have been cached by getting a property, saving a new value for
216 * the property, getting all properties (which clears the cached single
217 * properties), then getting the property again. The new value for the
218 * property rather than the cached value of the property should be
219 * returned.
220 */
221 public function testClearCache() {
222 $pageProps = PageProps::getInstance();
223 $page1ID = $this->title1->getArticleID();
224 $pageProps->getProperties( $this->title1, "property1" );
225 $new_value = "another value";
226 $this->setProperty( $page1ID, "property1", $new_value );
227 $pageProps->getAllProperties( $this->title1 );
228 $result = $pageProps->getProperties( $this->title1, "property1" );
229 $this->assertArrayHasKey( $page1ID, $result, "Found property" );
230 $this->assertEquals( $result[$page1ID], "another value", "Clear cache" );
231 }
232
233 protected function createPage( $page, $text, $model = null ) {
234 if ( is_string( $page ) ) {
235 if ( !preg_match( '/:/', $page ) &&
236 ( $model === null || $model === CONTENT_MODEL_WIKITEXT )
237 ) {
238 $ns = $this->getDefaultWikitextNS();
239 $page = MediaWikiServices::getInstance()->getNamespaceInfo()->
240 getCanonicalName( $ns ) . ':' . $page;
241 }
242
243 $page = Title::newFromText( $page );
244 }
245
246 if ( $page instanceof Title ) {
247 $page = new WikiPage( $page );
248 }
249
250 if ( $page->exists() ) {
251 $page->doDeleteArticle( "done" );
252 }
253
254 $content = ContentHandler::makeContent( $text, $page->getTitle(), $model );
255 $page->doEditContent( $content, "testing", EDIT_NEW );
256
257 return $page;
258 }
259
260 protected function setProperties( $pageID, $properties ) {
261 $rows = [];
262
263 foreach ( $properties as $propertyName => $propertyValue ) {
264 $row = [
265 'pp_page' => $pageID,
266 'pp_propname' => $propertyName,
267 'pp_value' => $propertyValue
268 ];
269
270 $rows[] = $row;
271 }
272
273 $dbw = wfGetDB( DB_MASTER );
274 $dbw->replace(
275 'page_props',
276 [
277 [
278 'pp_page',
279 'pp_propname'
280 ]
281 ],
282 $rows,
283 __METHOD__
284 );
285 }
286
287 protected function setProperty( $pageID, $propertyName, $propertyValue ) {
288 $properties = [];
289 $properties[$propertyName] = $propertyValue;
290
291 $this->setProperties( $pageID, $properties );
292 }
293 }