2016-01-12 04:29:48 +00:00
|
|
|
<?php
|
|
|
|
|
|
2018-08-05 17:58:51 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
|
|
2016-01-12 04:29:48 +00:00
|
|
|
/**
|
2017-12-25 07:28:03 +00:00
|
|
|
* @covers PageProps
|
|
|
|
|
*
|
2016-01-12 04:29:48 +00:00
|
|
|
* @group Database
|
|
|
|
|
* ^--- tell jenkins this test needs the database
|
|
|
|
|
*
|
|
|
|
|
* @group medium
|
|
|
|
|
* ^--- tell phpunit that these test cases may take longer than 2 seconds.
|
|
|
|
|
*/
|
2017-12-10 10:40:05 +00:00
|
|
|
class PagePropsTest extends MediaWikiLangTestCase {
|
2016-01-12 04:29:48 +00:00
|
|
|
|
|
|
|
|
/**
|
2019-11-29 13:33:43 +00:00
|
|
|
* @var Title
|
2016-01-12 04:29:48 +00:00
|
|
|
*/
|
|
|
|
|
private $title1;
|
|
|
|
|
|
|
|
|
|
/**
|
2019-11-29 13:33:43 +00:00
|
|
|
* @var Title
|
2016-01-12 04:29:48 +00:00
|
|
|
*/
|
|
|
|
|
private $title2;
|
|
|
|
|
|
|
|
|
|
/**
|
2019-11-29 13:33:43 +00:00
|
|
|
* @var array
|
2016-01-12 04:29:48 +00:00
|
|
|
*/
|
2020-03-06 20:29:47 +00:00
|
|
|
private $expectedProperties;
|
2016-01-12 04:29:48 +00:00
|
|
|
|
2019-10-20 18:11:08 +00:00
|
|
|
protected function setUp() : void {
|
2016-01-12 04:29:48 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
|
2018-08-01 12:40:47 +00:00
|
|
|
$this->setMwGlobals( [
|
|
|
|
|
'wgExtraNamespaces' => [
|
|
|
|
|
12312 => 'Dummy',
|
|
|
|
|
12313 => 'Dummy_talk',
|
|
|
|
|
],
|
|
|
|
|
'wgNamespaceContentModels' => [ 12312 => 'DUMMY' ],
|
|
|
|
|
] );
|
2016-01-12 04:29:48 +00:00
|
|
|
|
2018-08-01 12:40:47 +00:00
|
|
|
$this->mergeMwGlobalArrayValue(
|
|
|
|
|
'wgContentHandlers',
|
|
|
|
|
[ 'DUMMY' => 'DummyContentHandlerForTesting' ]
|
|
|
|
|
);
|
2016-01-12 04:29:48 +00:00
|
|
|
|
2020-03-06 20:29:47 +00:00
|
|
|
if ( !$this->expectedProperties ) {
|
|
|
|
|
$this->expectedProperties = [
|
2016-01-12 04:29:48 +00:00
|
|
|
"property1" => "value1",
|
|
|
|
|
"property2" => "value2",
|
|
|
|
|
"property3" => "value3",
|
|
|
|
|
"property4" => "value4"
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2016-01-12 04:29:48 +00:00
|
|
|
|
|
|
|
|
$page = $this->createPage(
|
|
|
|
|
'PagePropsTest_page_1',
|
|
|
|
|
"just a dummy page",
|
|
|
|
|
CONTENT_MODEL_WIKITEXT
|
|
|
|
|
);
|
|
|
|
|
$this->title1 = $page->getTitle();
|
|
|
|
|
$page1ID = $this->title1->getArticleID();
|
2020-03-06 20:29:47 +00:00
|
|
|
$this->setProperties( $page1ID, $this->expectedProperties );
|
2016-01-12 04:29:48 +00:00
|
|
|
|
|
|
|
|
$page = $this->createPage(
|
|
|
|
|
'PagePropsTest_page_2',
|
|
|
|
|
"just a dummy page",
|
|
|
|
|
CONTENT_MODEL_WIKITEXT
|
|
|
|
|
);
|
|
|
|
|
$this->title2 = $page->getTitle();
|
|
|
|
|
$page2ID = $this->title2->getArticleID();
|
2020-03-06 20:29:47 +00:00
|
|
|
$this->setProperties( $page2ID, $this->expectedProperties );
|
2016-01-12 04:29:48 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test getting a single property from a single page. The property was
|
|
|
|
|
* set in setUp().
|
|
|
|
|
*/
|
|
|
|
|
public function testGetSingleProperty() {
|
|
|
|
|
$pageProps = PageProps::getInstance();
|
|
|
|
|
$page1ID = $this->title1->getArticleID();
|
2016-02-14 21:58:46 +00:00
|
|
|
$result = $pageProps->getProperties( $this->title1, "property1" );
|
2016-01-12 04:29:48 +00:00
|
|
|
$this->assertArrayHasKey( $page1ID, $result, "Found property" );
|
|
|
|
|
$this->assertEquals( $result[$page1ID], "value1", "Get property" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test getting a single property from multiple pages. The property was
|
|
|
|
|
* set in setUp().
|
|
|
|
|
*/
|
|
|
|
|
public function testGetSinglePropertyMultiplePages() {
|
|
|
|
|
$pageProps = PageProps::getInstance();
|
|
|
|
|
$page1ID = $this->title1->getArticleID();
|
|
|
|
|
$page2ID = $this->title2->getArticleID();
|
2016-02-17 09:09:32 +00:00
|
|
|
$titles = [
|
2016-01-12 04:29:48 +00:00
|
|
|
$this->title1,
|
|
|
|
|
$this->title2
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2016-02-14 21:58:46 +00:00
|
|
|
$result = $pageProps->getProperties( $titles, "property1" );
|
2016-01-12 04:29:48 +00:00
|
|
|
$this->assertArrayHasKey( $page1ID, $result, "Found page 1 property" );
|
|
|
|
|
$this->assertArrayHasKey( $page2ID, $result, "Found page 2 property" );
|
|
|
|
|
$this->assertEquals( $result[$page1ID], "value1", "Get property page 1" );
|
|
|
|
|
$this->assertEquals( $result[$page2ID], "value1", "Get property page 2" );
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-14 21:58:46 +00:00
|
|
|
/**
|
|
|
|
|
* Test getting multiple properties from multiple pages. The properties
|
|
|
|
|
* were set in setUp().
|
|
|
|
|
*/
|
|
|
|
|
public function testGetMultiplePropertiesMultiplePages() {
|
|
|
|
|
$pageProps = PageProps::getInstance();
|
|
|
|
|
$page1ID = $this->title1->getArticleID();
|
|
|
|
|
$page2ID = $this->title2->getArticleID();
|
2016-02-17 09:09:32 +00:00
|
|
|
$titles = [
|
2016-02-14 21:58:46 +00:00
|
|
|
$this->title1,
|
|
|
|
|
$this->title2
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
|
|
|
|
$properties = [
|
2016-02-14 21:58:46 +00:00
|
|
|
"property1",
|
|
|
|
|
"property2"
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2016-02-14 21:58:46 +00:00
|
|
|
$result = $pageProps->getProperties( $titles, $properties );
|
|
|
|
|
$this->assertArrayHasKey( $page1ID, $result, "Found page 1 property" );
|
|
|
|
|
$this->assertArrayHasKey( "property1", $result[$page1ID], "Found page 1 property 1" );
|
|
|
|
|
$this->assertArrayHasKey( "property2", $result[$page1ID], "Found page 1 property 2" );
|
|
|
|
|
$this->assertArrayHasKey( $page2ID, $result, "Found page 2 property" );
|
|
|
|
|
$this->assertArrayHasKey( "property1", $result[$page2ID], "Found page 2 property 1" );
|
|
|
|
|
$this->assertArrayHasKey( "property2", $result[$page2ID], "Found page 2 property 2" );
|
|
|
|
|
$this->assertEquals( $result[$page1ID]["property1"], "value1", "Get page 1 property 1" );
|
|
|
|
|
$this->assertEquals( $result[$page1ID]["property2"], "value2", "Get page 1 property 2" );
|
|
|
|
|
$this->assertEquals( $result[$page2ID]["property1"], "value1", "Get page 2 property 1" );
|
|
|
|
|
$this->assertEquals( $result[$page2ID]["property2"], "value2", "Get page 2 property 2" );
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-12 04:29:48 +00:00
|
|
|
/**
|
|
|
|
|
* Test getting all properties from a single page. The properties were
|
|
|
|
|
* set in setUp(). The properties retrieved from the page may include
|
|
|
|
|
* additional properties not set in the test case that are added by
|
|
|
|
|
* other extensions. Therefore, rather than checking to see if the
|
|
|
|
|
* properties that were set in the test case exactly match the
|
|
|
|
|
* retrieved properties, we need to check to see if they are a
|
2020-03-06 20:29:47 +00:00
|
|
|
* subset of the retrieved properties.
|
2016-01-12 04:29:48 +00:00
|
|
|
*/
|
|
|
|
|
public function testGetAllProperties() {
|
|
|
|
|
$pageProps = PageProps::getInstance();
|
|
|
|
|
$page1ID = $this->title1->getArticleID();
|
2016-02-14 21:58:46 +00:00
|
|
|
$result = $pageProps->getAllProperties( $this->title1 );
|
2016-01-12 04:29:48 +00:00
|
|
|
$this->assertArrayHasKey( $page1ID, $result, "Found properties" );
|
2020-03-06 20:29:47 +00:00
|
|
|
|
2016-01-12 04:29:48 +00:00
|
|
|
$properties = $result[$page1ID];
|
2020-03-06 20:29:47 +00:00
|
|
|
$subset = array_intersect_key( $properties, $this->expectedProperties );
|
|
|
|
|
$this->assertEquals( $this->expectedProperties, $subset, "Get all properties" );
|
2016-01-12 04:29:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test getting all properties from multiple pages. The properties were
|
|
|
|
|
* set in setUp(). See getAllProperties() above for more information.
|
|
|
|
|
*/
|
|
|
|
|
public function testGetAllPropertiesMultiplePages() {
|
|
|
|
|
$pageProps = PageProps::getInstance();
|
|
|
|
|
$page1ID = $this->title1->getArticleID();
|
|
|
|
|
$page2ID = $this->title2->getArticleID();
|
2016-02-17 09:09:32 +00:00
|
|
|
$titles = [
|
2016-01-12 04:29:48 +00:00
|
|
|
$this->title1,
|
|
|
|
|
$this->title2
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2016-02-14 21:58:46 +00:00
|
|
|
$result = $pageProps->getAllProperties( $titles );
|
2016-01-12 04:29:48 +00:00
|
|
|
$this->assertArrayHasKey( $page1ID, $result, "Found page 1 properties" );
|
|
|
|
|
$this->assertArrayHasKey( $page2ID, $result, "Found page 2 properties" );
|
2020-03-06 20:29:47 +00:00
|
|
|
|
|
|
|
|
$properties = $result[$page1ID];
|
|
|
|
|
$subset = array_intersect_key( $properties, $this->expectedProperties );
|
|
|
|
|
$this->assertEquals( $this->expectedProperties, $subset, "Properties of page 1" );
|
|
|
|
|
|
|
|
|
|
$properties = $result[$page2ID];
|
|
|
|
|
$subset = array_intersect_key( $properties, $this->expectedProperties );
|
|
|
|
|
$this->assertEquals( $this->expectedProperties, $subset, "Properties of page 2" );
|
2016-01-12 04:29:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test caching when retrieving single properties by getting a property,
|
|
|
|
|
* saving a new value for the property, then getting the property
|
|
|
|
|
* again. The cached value for the property rather than the new value
|
|
|
|
|
* of the property should be returned.
|
|
|
|
|
*/
|
|
|
|
|
public function testSingleCache() {
|
|
|
|
|
$pageProps = PageProps::getInstance();
|
|
|
|
|
$page1ID = $this->title1->getArticleID();
|
2016-02-14 21:58:46 +00:00
|
|
|
$value1 = $pageProps->getProperties( $this->title1, "property1" );
|
2016-01-12 04:29:48 +00:00
|
|
|
$this->setProperty( $page1ID, "property1", "another value" );
|
2016-02-14 21:58:46 +00:00
|
|
|
$value2 = $pageProps->getProperties( $this->title1, "property1" );
|
2020-03-06 20:29:47 +00:00
|
|
|
|
2016-01-12 04:29:48 +00:00
|
|
|
$this->assertEquals( $value1, $value2, "Single cache" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test caching when retrieving all properties by getting all
|
|
|
|
|
* properties, saving a new value for a property, then getting all
|
|
|
|
|
* properties again. The cached value for the properties rather than the
|
|
|
|
|
* new value of the properties should be returned.
|
|
|
|
|
*/
|
|
|
|
|
public function testMultiCache() {
|
|
|
|
|
$pageProps = PageProps::getInstance();
|
|
|
|
|
$page1ID = $this->title1->getArticleID();
|
2016-02-14 21:58:46 +00:00
|
|
|
$properties1 = $pageProps->getAllProperties( $this->title1 );
|
2016-01-12 04:29:48 +00:00
|
|
|
$this->setProperty( $page1ID, "property1", "another value" );
|
2016-02-14 21:58:46 +00:00
|
|
|
$properties2 = $pageProps->getAllProperties( $this->title1 );
|
2020-03-06 20:29:47 +00:00
|
|
|
|
2016-01-12 04:29:48 +00:00
|
|
|
$this->assertEquals( $properties1, $properties2, "Multi Cache" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test that getting all properties clears the single properties
|
|
|
|
|
* that have been cached by getting a property, saving a new value for
|
|
|
|
|
* the property, getting all properties (which clears the cached single
|
|
|
|
|
* properties), then getting the property again. The new value for the
|
|
|
|
|
* property rather than the cached value of the property should be
|
|
|
|
|
* returned.
|
|
|
|
|
*/
|
|
|
|
|
public function testClearCache() {
|
|
|
|
|
$pageProps = PageProps::getInstance();
|
|
|
|
|
$page1ID = $this->title1->getArticleID();
|
2016-02-14 21:58:46 +00:00
|
|
|
$pageProps->getProperties( $this->title1, "property1" );
|
2016-01-12 04:29:48 +00:00
|
|
|
$new_value = "another value";
|
|
|
|
|
$this->setProperty( $page1ID, "property1", $new_value );
|
2016-02-14 21:58:46 +00:00
|
|
|
$pageProps->getAllProperties( $this->title1 );
|
|
|
|
|
$result = $pageProps->getProperties( $this->title1, "property1" );
|
2016-01-12 04:29:48 +00:00
|
|
|
$this->assertArrayHasKey( $page1ID, $result, "Found property" );
|
|
|
|
|
$this->assertEquals( $result[$page1ID], "another value", "Clear cache" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function createPage( $page, $text, $model = null ) {
|
|
|
|
|
if ( is_string( $page ) ) {
|
|
|
|
|
if ( !preg_match( '/:/', $page ) &&
|
|
|
|
|
( $model === null || $model === CONTENT_MODEL_WIKITEXT )
|
|
|
|
|
) {
|
|
|
|
|
$ns = $this->getDefaultWikitextNS();
|
2018-08-05 17:58:51 +00:00
|
|
|
$page = MediaWikiServices::getInstance()->getNamespaceInfo()->
|
|
|
|
|
getCanonicalName( $ns ) . ':' . $page;
|
2016-01-12 04:29:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$page = Title::newFromText( $page );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $page instanceof Title ) {
|
|
|
|
|
$page = new WikiPage( $page );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $page->exists() ) {
|
2020-03-25 17:05:26 +00:00
|
|
|
$page->doDeleteArticleReal( "done", $this->getTestSysop()->getUser() );
|
2016-01-12 04:29:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$content = ContentHandler::makeContent( $text, $page->getTitle(), $model );
|
|
|
|
|
$page->doEditContent( $content, "testing", EDIT_NEW );
|
|
|
|
|
|
|
|
|
|
return $page;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function setProperties( $pageID, $properties ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$rows = [];
|
2016-01-12 04:29:48 +00:00
|
|
|
foreach ( $properties as $propertyName => $propertyValue ) {
|
2020-03-06 20:29:47 +00:00
|
|
|
$rows[] = [
|
2016-01-12 04:29:48 +00:00
|
|
|
'pp_page' => $pageID,
|
|
|
|
|
'pp_propname' => $propertyName,
|
|
|
|
|
'pp_value' => $propertyValue
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2016-01-12 04:29:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
|
|
|
|
$dbw->replace(
|
|
|
|
|
'page_props',
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
|
|
|
|
[
|
2016-01-12 04:29:48 +00:00
|
|
|
'pp_page',
|
|
|
|
|
'pp_propname'
|
2016-02-17 09:09:32 +00:00
|
|
|
]
|
|
|
|
|
],
|
2016-01-12 04:29:48 +00:00
|
|
|
$rows,
|
|
|
|
|
__METHOD__
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function setProperty( $pageID, $propertyName, $propertyValue ) {
|
2020-03-06 20:29:47 +00:00
|
|
|
$properties = [
|
|
|
|
|
$propertyName => $propertyValue
|
|
|
|
|
];
|
2016-01-12 04:29:48 +00:00
|
|
|
$this->setProperties( $pageID, $properties );
|
|
|
|
|
}
|
|
|
|
|
}
|