Merge ".mailmap: Correct two contributor names"
[mediawiki.git] / tests / phpunit / includes / page / PagePropsTest.php
blobdeb7f1bb138be00dca381e7fe9d7e3f296ac659d
1 <?php
3 use MediaWiki\Title\Title;
4 use Wikimedia\Rdbms\FakeResultWrapper;
6 /**
7 * @covers \MediaWiki\Page\PageProps
8 * @group Database
9 * @group medium
11 class PagePropsTest extends MediaWikiLangTestCase {
13 private ?array $expectedProperties = null;
14 private Title $title1;
15 private Title $title2;
17 protected function setUp(): void {
18 parent::setUp();
20 if ( !$this->expectedProperties ) {
21 $this->expectedProperties = [
22 "property1" => "value1",
23 "property2" => "value2",
24 "property3" => "value3",
25 "property4" => "value4"
28 $page = $this->getExistingTestPage( 'PagePropsTest_page_1' );
29 $this->title1 = $page->getTitle();
30 $page1ID = $this->title1->getArticleID();
31 $this->setProperties( $page1ID, $this->expectedProperties );
33 $page = $this->getExistingTestPage( 'PagePropsTest_page_2' );
34 $this->title2 = $page->getTitle();
35 $page2ID = $this->title2->getArticleID();
36 $this->setProperties( $page2ID, $this->expectedProperties );
40 /**
41 * Test getting a single property from a single page. The property was
42 * set in setUp().
44 public function testGetSingleProperty() {
45 $pageProps = $this->getServiceContainer()->getPageProps();
46 $page1ID = $this->title1->getArticleID();
47 $result = $pageProps->getProperties( $this->title1, "property1" );
48 $this->assertArrayHasKey( $page1ID, $result, "Found property" );
49 $this->assertSame( "value1", $result[$page1ID], "Get property" );
52 /**
53 * Test getting a single property from multiple pages. The property was
54 * set in setUp(). Using Title[].
56 public function testGetSinglePropertyMultiplePages() {
57 $pageProps = $this->getServiceContainer()->getPageProps();
58 $page1ID = $this->title1->getArticleID();
59 $page2ID = $this->title2->getArticleID();
60 $titles = [
61 $this->title1,
62 $this->title2
64 $result = $pageProps->getProperties( $titles, "property1" );
65 $this->assertArrayHasKey( $page1ID, $result, "Found page 1 property" );
66 $this->assertArrayHasKey( $page2ID, $result, "Found page 2 property" );
67 $this->assertSame( "value1", $result[$page1ID], "Get property page 1" );
68 $this->assertSame( "value1", $result[$page2ID], "Get property page 2" );
71 /**
72 * Test getting a single property from multiple pages. The property was
73 * set in setUp(). Using TitleArray.
75 public function testGetSinglePropertyMultiplePagesTitleArray() {
76 $services = $this->getServiceContainer();
77 $pageProps = $services->getPageProps();
78 $page1ID = $this->title1->getArticleID();
79 $page2ID = $this->title2->getArticleID();
80 $rows = [
81 $this->createRowFromTitle( $this->title1 ),
82 $this->createRowFromTitle( $this->title2 )
84 $resultWrapper = new FakeResultWrapper( $rows );
85 $titles = $services->getTitleFactory()->newTitleArrayFromResult( $resultWrapper );
86 $result = $pageProps->getProperties( $titles, "property1" );
87 $this->assertArrayHasKey( $page1ID, $result, "Found page 1 property" );
88 $this->assertArrayHasKey( $page2ID, $result, "Found page 2 property" );
89 $this->assertSame( "value1", $result[$page1ID], "Get property page 1" );
90 $this->assertSame( "value1", $result[$page2ID], "Get property page 2" );
93 /**
94 * Test getting multiple properties from multiple pages. The properties
95 * were set in setUp().
97 public function testGetMultiplePropertiesMultiplePages() {
98 $pageProps = $this->getServiceContainer()->getPageProps();
99 $page1ID = $this->title1->getArticleID();
100 $page2ID = $this->title2->getArticleID();
101 $titles = [
102 $this->title1->toPageIdentity(),
103 $this->title2->toPageIdentity()
105 $properties = [
106 "property1",
107 "property2"
109 $result = $pageProps->getProperties( $titles, $properties );
110 $this->assertArrayHasKey( $page1ID, $result, "Found page 1 property" );
111 $this->assertArrayHasKey( "property1", $result[$page1ID], "Found page 1 property 1" );
112 $this->assertArrayHasKey( "property2", $result[$page1ID], "Found page 1 property 2" );
113 $this->assertArrayHasKey( $page2ID, $result, "Found page 2 property" );
114 $this->assertArrayHasKey( "property1", $result[$page2ID], "Found page 2 property 1" );
115 $this->assertArrayHasKey( "property2", $result[$page2ID], "Found page 2 property 2" );
116 $this->assertSame( "value1", $result[$page1ID]["property1"], "Get page 1 property 1" );
117 $this->assertSame( "value2", $result[$page1ID]["property2"], "Get page 1 property 2" );
118 $this->assertSame( "value1", $result[$page2ID]["property1"], "Get page 2 property 1" );
119 $this->assertSame( "value2", $result[$page2ID]["property2"], "Get page 2 property 2" );
123 * Test getting all properties from a single page. The properties were
124 * set in setUp(). The properties retrieved from the page may include
125 * additional properties not set in the test case that are added by
126 * other extensions. Therefore, rather than checking to see if the
127 * properties that were set in the test case exactly match the
128 * retrieved properties, we need to check to see if they are a
129 * subset of the retrieved properties.
131 public function testGetAllProperties() {
132 $pageProps = $this->getServiceContainer()->getPageProps();
133 $page1ID = $this->title1->getArticleID();
134 $result = $pageProps->getAllProperties( $this->title1 );
135 $this->assertArrayHasKey( $page1ID, $result, "Found properties" );
137 $properties = $result[$page1ID];
138 $subset = array_intersect_key( $properties, $this->expectedProperties );
139 $this->assertEquals( $this->expectedProperties, $subset, "Get all properties" );
143 * Test getting all properties from multiple pages. The properties were
144 * set in setUp(). See getAllProperties() above for more information.
146 public function testGetAllPropertiesMultiplePages() {
147 $pageProps = $this->getServiceContainer()->getPageProps();
148 $page1ID = $this->title1->getArticleID();
149 $page2ID = $this->title2->getArticleID();
150 $titles = [
151 $this->title1,
152 $this->title2
154 $result = $pageProps->getAllProperties( $titles );
155 $this->assertArrayHasKey( $page1ID, $result, "Found page 1 properties" );
156 $this->assertArrayHasKey( $page2ID, $result, "Found page 2 properties" );
158 $properties = $result[$page1ID];
159 $subset = array_intersect_key( $properties, $this->expectedProperties );
160 $this->assertEquals( $this->expectedProperties, $subset, "Properties of page 1" );
162 $properties = $result[$page2ID];
163 $subset = array_intersect_key( $properties, $this->expectedProperties );
164 $this->assertEquals( $this->expectedProperties, $subset, "Properties of page 2" );
168 * Test caching when retrieving single properties by getting a property,
169 * saving a new value for the property, then getting the property
170 * again. The cached value for the property rather than the new value
171 * of the property should be returned.
173 public function testSingleCache() {
174 $pageProps = $this->getServiceContainer()->getPageProps();
175 $page1ID = $this->title1->getArticleID();
176 $value1 = $pageProps->getProperties( $this->title1, "property1" );
177 $this->setProperty( $page1ID, "property1", "another value" );
178 $value2 = $pageProps->getProperties( $this->title1, "property1" );
180 $this->assertEquals( $value1, $value2, "Single cache" );
184 * Test caching when retrieving all properties by getting all
185 * properties, saving a new value for a property, then getting all
186 * properties again. The cached value for the properties rather than the
187 * new value of the properties should be returned.
189 public function testMultiCache() {
190 $pageProps = $this->getServiceContainer()->getPageProps();
191 $page1ID = $this->title1->getArticleID();
192 $properties1 = $pageProps->getAllProperties( $this->title1 );
193 $this->setProperty( $page1ID, "property1", "another value" );
194 $properties2 = $pageProps->getAllProperties( $this->title1 );
196 $this->assertEquals( $properties1, $properties2, "Multi Cache" );
200 * Test that getting all properties clears the single properties
201 * that have been cached by getting a property, saving a new value for
202 * the property, getting all properties (which clears the cached single
203 * properties), then getting the property again. The new value for the
204 * property rather than the cached value of the property should be
205 * returned.
207 public function testClearCache() {
208 $pageProps = $this->getServiceContainer()->getPageProps();
209 $page1ID = $this->title1->getArticleID();
210 $pageProps->getProperties( $this->title1, "property1" );
211 $new_value = "another value";
212 $this->setProperty( $page1ID, "property1", $new_value );
213 $pageProps->getAllProperties( $this->title1 );
214 $result = $pageProps->getProperties( $this->title1, "property1" );
215 $this->assertArrayHasKey( $page1ID, $result, "Found property" );
216 $this->assertSame( "another value", $result[$page1ID], "Clear cache" );
219 protected function setProperties( $pageID, $properties ) {
220 $queryBuilder = $this->getDb()->newReplaceQueryBuilder()
221 ->replaceInto( 'page_props' )
222 ->uniqueIndexFields( [ 'pp_page', 'pp_propname' ] );
223 foreach ( $properties as $propertyName => $propertyValue ) {
224 $queryBuilder->row( [
225 'pp_page' => $pageID,
226 'pp_propname' => $propertyName,
227 'pp_value' => $propertyValue
228 ] );
230 $queryBuilder->caller( __METHOD__ )->execute();
233 protected function setProperty( $pageID, $propertyName, $propertyValue ) {
234 $properties = [
235 $propertyName => $propertyValue
237 $this->setProperties( $pageID, $properties );
240 protected function createRowFromTitle( $title ) {
241 return (object)[
242 'page_namespace' => $title->getNamespace(),
243 'page_title' => $title->getText()