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