mediawiki-extensions-Echo/tests/phpunit/includes/cache/TitleLocalCacheTest.php
Antoine Musso 7f5b0c1793 Fix EchoTitleLocalCacheTest::testGet corner case
EchoTitleLocalCache title cache resolution ends up querying the database
to resolve title id to Title object.

In some corner case, we might only have one page in the database (UTPage
as provided by MediaWikiTestCase), thus the id 2 would not resolve to a
Title breaking the test.

Use insertPage() to ensure we have a second page.

Bug: T78592
Change-Id: Ia9dbb256f566e489e1c81d89b6a6077831e07fc3
2015-01-09 13:54:08 +01:00

79 lines
2.4 KiB
PHP

<?php
class EchoTitleLocalCacheTest extends MediaWikiTestCase {
public function testCreate() {
$cache = EchoTitleLocalCache::create();
$this->assertInstanceOf( 'EchoTitleLocalCache', $cache );
return $cache;
}
/**
* @depends testCreate
*/
public function testAdd( $cache ) {
$cache->add( 1 );
$this->assertEquals( count( $cache->getLookups() ), 1 );
$this->assertArrayHasKey( 1, $cache->getLookups() );
}
/**
* @depends testCreate
*/
public function testGet( $cache ) {
$object = new \ReflectionObject( $cache );
$targets = $object->getProperty( 'targets' );
$targets->setAccessible( true );
$lruMap = new MapCacheLRU( EchoLocalCache::TARGET_MAX_NUM );
$lruMap->set( 1, $this->mockTitle() );
$targets->setValue( $cache, $lruMap );
$lookups = $object->getProperty( 'lookups' );
$lookups->setAccessible( true );
$lookups->setValue( $cache, array( '1' => '1', '2' => '2' ) );
# A second page in addition to MediaWikiTestPage 'UTPage' since
# TitleLocalCache internally invokes Title::newFromIDs()
$this->insertPage('EchoTitleLocalCacheTest_testGet');
$titles = Title::newFromIDs( array( '1', '2' ) );
$this->assertEquals( 2, count( $titles ), "Must have at least two pages" );
$this->assertEquals( array(1 => '1', 2 => '2' ), $cache->getLookups() );
// MapCacheLRU should treat key 1 same as '1'
$this->assertInstanceOf( 'Title', $cache->get( '1' ) );
$this->assertTrue( count( $cache->getLookups() ) > 0 );
$this->assertInstanceOf( 'Title', $cache->get( 2 ) );
$this->assertTrue( count( $cache->getLookups() ) == 0 );
}
/**
* @depends testCreate
*/
public function testClearAll( $cache ) {
$object = new \ReflectionObject( $cache );
$targets = $object->getProperty( 'targets' );
$targets->setAccessible( true );
$lruMap = new MapCacheLRU( EchoLocalCache::TARGET_MAX_NUM );
$lruMap->set( 1, $this->mockTitle() );
$targets->setValue( $cache, $lruMap );
$lookups = $object->getProperty( 'lookups' );
$lookups->setAccessible( true );
$lookups->setValue( $cache, array( '1' => '1', '2' => '2' ) );
$cache->clearAll();
$this->assertTrue( count( $cache->getLookups() ) == 0 );
$this->assertNull( $cache->getTargets()->get( 1 ) );
$this->assertNull( $cache->getTargets()->get( '1' ) );
}
/**
* Mock object of Title
*/
protected function mockTitle() {
$title = $this->getMockBuilder( 'Title' )
->disableOriginalConstructor()
->getMock();
return $title;
}
}