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