Move ResultWrapper subclasses to Rdbms
[mediawiki.git] / tests / phpunit / includes / PagePropsTest.php
blob29c9e228f2cebf1851d5f0c76495bdfa6d0ef019
1 <?php
3 /**
4 * @group Database
5 * ^--- tell jenkins this test needs the database
7 * @group medium
8 * ^--- tell phpunit that these test cases may take longer than 2 seconds.
9 */
10 class TestPageProps extends MediaWikiLangTestCase {
12 /**
13 * @var Title $title1
15 private $title1;
17 /**
18 * @var Title $title2
20 private $title2;
22 /**
23 * @var array $the_properties
25 private $the_properties;
27 protected function setUp() {
28 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
30 parent::setUp();
32 $wgExtraNamespaces[12312] = 'Dummy';
33 $wgExtraNamespaces[12313] = 'Dummy_talk';
35 $wgNamespaceContentModels[12312] = 'DUMMY';
36 $wgContentHandlers['DUMMY'] = 'DummyContentHandlerForTesting';
38 MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
39 $wgContLang->resetNamespaces(); # reset namespace cache
41 if ( !$this->the_properties ) {
42 $this->the_properties = [
43 "property1" => "value1",
44 "property2" => "value2",
45 "property3" => "value3",
46 "property4" => "value4"
50 if ( !$this->title1 ) {
51 $page = $this->createPage(
52 'PagePropsTest_page_1',
53 "just a dummy page",
54 CONTENT_MODEL_WIKITEXT
56 $this->title1 = $page->getTitle();
57 $page1ID = $this->title1->getArticleID();
58 $this->setProperties( $page1ID, $this->the_properties );
61 if ( !$this->title2 ) {
62 $page = $this->createPage(
63 'PagePropsTest_page_2',
64 "just a dummy page",
65 CONTENT_MODEL_WIKITEXT
67 $this->title2 = $page->getTitle();
68 $page2ID = $this->title2->getArticleID();
69 $this->setProperties( $page2ID, $this->the_properties );
73 protected function tearDown() {
74 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
76 parent::tearDown();
78 unset( $wgExtraNamespaces[12312] );
79 unset( $wgExtraNamespaces[12313] );
81 unset( $wgNamespaceContentModels[12312] );
82 unset( $wgContentHandlers['DUMMY'] );
84 MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
85 $wgContLang->resetNamespaces(); # reset namespace cache
88 /**
89 * Test getting a single property from a single page. The property was
90 * set in setUp().
92 public function testGetSingleProperty() {
93 $pageProps = PageProps::getInstance();
94 $page1ID = $this->title1->getArticleID();
95 $result = $pageProps->getProperties( $this->title1, "property1" );
96 $this->assertArrayHasKey( $page1ID, $result, "Found property" );
97 $this->assertEquals( $result[$page1ID], "value1", "Get property" );
101 * Test getting a single property from multiple pages. The property was
102 * set in setUp().
104 public function testGetSinglePropertyMultiplePages() {
105 $pageProps = PageProps::getInstance();
106 $page1ID = $this->title1->getArticleID();
107 $page2ID = $this->title2->getArticleID();
108 $titles = [
109 $this->title1,
110 $this->title2
112 $result = $pageProps->getProperties( $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" );
120 * Test getting multiple properties from multiple pages. The properties
121 * were set in setUp().
123 public function testGetMultiplePropertiesMultiplePages() {
124 $pageProps = PageProps::getInstance();
125 $page1ID = $this->title1->getArticleID();
126 $page2ID = $this->title2->getArticleID();
127 $titles = [
128 $this->title1,
129 $this->title2
131 $properties = [
132 "property1",
133 "property2"
135 $result = $pageProps->getProperties( $titles, $properties );
136 $this->assertArrayHasKey( $page1ID, $result, "Found page 1 property" );
137 $this->assertArrayHasKey( "property1", $result[$page1ID], "Found page 1 property 1" );
138 $this->assertArrayHasKey( "property2", $result[$page1ID], "Found page 1 property 2" );
139 $this->assertArrayHasKey( $page2ID, $result, "Found page 2 property" );
140 $this->assertArrayHasKey( "property1", $result[$page2ID], "Found page 2 property 1" );
141 $this->assertArrayHasKey( "property2", $result[$page2ID], "Found page 2 property 2" );
142 $this->assertEquals( $result[$page1ID]["property1"], "value1", "Get page 1 property 1" );
143 $this->assertEquals( $result[$page1ID]["property2"], "value2", "Get page 1 property 2" );
144 $this->assertEquals( $result[$page2ID]["property1"], "value1", "Get page 2 property 1" );
145 $this->assertEquals( $result[$page2ID]["property2"], "value2", "Get page 2 property 2" );
149 * Test getting all properties from a single page. The properties were
150 * set in setUp(). The properties retrieved from the page may include
151 * additional properties not set in the test case that are added by
152 * other extensions. Therefore, rather than checking to see if the
153 * properties that were set in the test case exactly match the
154 * retrieved properties, we need to check to see if they are a
155 * subset of the retrieved properties. Since this version of PHPUnit
156 * does not yet include assertArraySubset(), we needed to code the
157 * equivalent functionality.
159 public function testGetAllProperties() {
160 $pageProps = PageProps::getInstance();
161 $page1ID = $this->title1->getArticleID();
162 $result = $pageProps->getAllProperties( $this->title1 );
163 $this->assertArrayHasKey( $page1ID, $result, "Found properties" );
164 $properties = $result[$page1ID];
165 $patched = array_replace_recursive( $properties, $this->the_properties );
166 $this->assertEquals( $patched, $properties, "Get all properties" );
170 * Test getting all properties from multiple pages. The properties were
171 * set in setUp(). See getAllProperties() above for more information.
173 public function testGetAllPropertiesMultiplePages() {
174 $pageProps = PageProps::getInstance();
175 $page1ID = $this->title1->getArticleID();
176 $page2ID = $this->title2->getArticleID();
177 $titles = [
178 $this->title1,
179 $this->title2
181 $result = $pageProps->getAllProperties( $titles );
182 $this->assertArrayHasKey( $page1ID, $result, "Found page 1 properties" );
183 $this->assertArrayHasKey( $page2ID, $result, "Found page 2 properties" );
184 $properties1 = $result[$page1ID];
185 $patched = array_replace_recursive( $properties1, $this->the_properties );
186 $this->assertEquals( $patched, $properties1, "Get all properties page 1" );
187 $properties2 = $result[$page2ID];
188 $patched = array_replace_recursive( $properties2, $this->the_properties );
189 $this->assertEquals( $patched, $properties2, "Get all properties page 2" );
193 * Test caching when retrieving single properties by getting a property,
194 * saving a new value for the property, then getting the property
195 * again. The cached value for the property rather than the new value
196 * of the property should be returned.
198 public function testSingleCache() {
199 $pageProps = PageProps::getInstance();
200 $page1ID = $this->title1->getArticleID();
201 $value1 = $pageProps->getProperties( $this->title1, "property1" );
202 $this->setProperty( $page1ID, "property1", "another value" );
203 $value2 = $pageProps->getProperties( $this->title1, "property1" );
204 $this->assertEquals( $value1, $value2, "Single cache" );
208 * Test caching when retrieving all properties by getting all
209 * properties, saving a new value for a property, then getting all
210 * properties again. The cached value for the properties rather than the
211 * new value of the properties should be returned.
213 public function testMultiCache() {
214 $pageProps = PageProps::getInstance();
215 $page1ID = $this->title1->getArticleID();
216 $properties1 = $pageProps->getAllProperties( $this->title1 );
217 $this->setProperty( $page1ID, "property1", "another value" );
218 $properties2 = $pageProps->getAllProperties( $this->title1 );
219 $this->assertEquals( $properties1, $properties2, "Multi Cache" );
223 * Test that getting all properties clears the single properties
224 * that have been cached by getting a property, saving a new value for
225 * the property, getting all properties (which clears the cached single
226 * properties), then getting the property again. The new value for the
227 * property rather than the cached value of the property should be
228 * returned.
230 public function testClearCache() {
231 $pageProps = PageProps::getInstance();
232 $page1ID = $this->title1->getArticleID();
233 $pageProps->getProperties( $this->title1, "property1" );
234 $new_value = "another value";
235 $this->setProperty( $page1ID, "property1", $new_value );
236 $pageProps->getAllProperties( $this->title1 );
237 $result = $pageProps->getProperties( $this->title1, "property1" );
238 $this->assertArrayHasKey( $page1ID, $result, "Found property" );
239 $this->assertEquals( $result[$page1ID], "another value", "Clear cache" );
242 protected function createPage( $page, $text, $model = null ) {
243 if ( is_string( $page ) ) {
244 if ( !preg_match( '/:/', $page ) &&
245 ( $model === null || $model === CONTENT_MODEL_WIKITEXT )
247 $ns = $this->getDefaultWikitextNS();
248 $page = MWNamespace::getCanonicalName( $ns ) . ':' . $page;
251 $page = Title::newFromText( $page );
254 if ( $page instanceof Title ) {
255 $page = new WikiPage( $page );
258 if ( $page->exists() ) {
259 $page->doDeleteArticle( "done" );
262 $content = ContentHandler::makeContent( $text, $page->getTitle(), $model );
263 $page->doEditContent( $content, "testing", EDIT_NEW );
265 return $page;
268 protected function setProperties( $pageID, $properties ) {
269 $rows = [];
271 foreach ( $properties as $propertyName => $propertyValue ) {
272 $row = [
273 'pp_page' => $pageID,
274 'pp_propname' => $propertyName,
275 'pp_value' => $propertyValue
278 $rows[] = $row;
281 $dbw = wfGetDB( DB_MASTER );
282 $dbw->replace(
283 'page_props',
286 'pp_page',
287 'pp_propname'
290 $rows,
291 __METHOD__
295 protected function setProperty( $pageID, $propertyName, $propertyValue ) {
296 $properties = [];
297 $properties[$propertyName] = $propertyValue;
299 $this->setProperties( $pageID, $properties );