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