2013-05-06 22:34:50 +00:00
|
|
|
<?php
|
|
|
|
|
2022-11-13 06:43:40 +00:00
|
|
|
namespace MediaWiki\Extension\Notifications\Test;
|
|
|
|
|
|
|
|
use HashBagOStuff;
|
|
|
|
use MediaWiki\Extension\Notifications\ArrayList;
|
|
|
|
use MediaWiki\Extension\Notifications\CachedList;
|
|
|
|
use MediaWiki\Extension\Notifications\ContainmentSet;
|
|
|
|
use MediaWiki\Extension\Notifications\OnWikiList;
|
|
|
|
use MediaWikiIntegrationTestCase;
|
|
|
|
use WANObjectCache;
|
|
|
|
|
2014-07-25 20:19:15 +00:00
|
|
|
/**
|
2022-11-13 06:43:40 +00:00
|
|
|
* @covers \MediaWiki\Extension\Notifications\ContainmentSet
|
2014-07-25 20:19:15 +00:00
|
|
|
* @group Echo
|
2018-09-14 19:26:45 +00:00
|
|
|
* @group Database
|
2014-07-25 20:19:15 +00:00
|
|
|
*/
|
2021-10-11 22:42:10 +00:00
|
|
|
class ContainmentSetTest extends MediaWikiIntegrationTestCase {
|
2013-05-06 22:34:50 +00:00
|
|
|
|
|
|
|
public function testGenericContains() {
|
2022-11-13 06:43:40 +00:00
|
|
|
$list = new ContainmentSet( self::getTestUser()->getUser() );
|
2013-05-06 22:34:50 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
$list->addArray( [ 'foo', 'bar' ] );
|
2013-05-06 22:34:50 +00:00
|
|
|
$this->assertTrue( $list->contains( 'foo' ) );
|
|
|
|
$this->assertTrue( $list->contains( 'bar' ) );
|
|
|
|
$this->assertFalse( $list->contains( 'whammo' ) );
|
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
$list->addArray( [ 'whammo' ] );
|
2013-05-06 22:34:50 +00:00
|
|
|
$this->assertTrue( $list->contains( 'whammo' ) );
|
2017-10-14 23:24:18 +00:00
|
|
|
|
|
|
|
$list->addArray( [ 0 ] );
|
|
|
|
$this->assertFalse( $list->contains( 'baz' ) );
|
2013-05-06 22:34:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCachedListInnerListIsOnlyCalledOnce() {
|
2022-11-12 06:37:37 +00:00
|
|
|
// simulate caching
|
|
|
|
$innerCache = new HashBagOStuff;
|
2019-02-28 21:17:15 +00:00
|
|
|
$wanCache = new WANObjectCache( [ 'cache' => $innerCache ] );
|
2013-05-06 22:34:50 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
$inner = [ 'bing', 'bang' ];
|
2013-05-06 22:34:50 +00:00
|
|
|
// We use a mock instead of the real thing for the $this->once() assertion
|
|
|
|
// verifying that the cache doesn't just keep asking the inner object
|
2022-11-13 06:43:40 +00:00
|
|
|
$list = $this->createMock( ArrayList::class );
|
2013-05-06 22:34:50 +00:00
|
|
|
$list->expects( $this->once() )
|
|
|
|
->method( 'getValues' )
|
2022-09-29 13:41:35 +00:00
|
|
|
->willReturn( $inner );
|
2022-10-03 18:20:43 +00:00
|
|
|
$list->method( 'getCacheKey' )->willReturn( '' );
|
2013-05-06 22:34:50 +00:00
|
|
|
|
2022-11-13 06:43:40 +00:00
|
|
|
$cached = new CachedList( $wanCache, 'test_key', $list );
|
2013-05-06 22:34:50 +00:00
|
|
|
|
|
|
|
// First run through should hit the main list, and save to innerCache
|
|
|
|
$this->assertEquals( $inner, $cached->getValues() );
|
|
|
|
$this->assertEquals( $inner, $cached->getValues() );
|
|
|
|
|
|
|
|
// Reinitialize to get a fresh instance that will pull directly from
|
|
|
|
// innerCache without hitting the $list
|
2022-11-13 06:43:40 +00:00
|
|
|
$freshCached = new CachedList( $wanCache, 'test_key', $list );
|
2013-05-06 22:34:50 +00:00
|
|
|
$this->assertEquals( $inner, $freshCached->getValues() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-09-09 18:41:51 +00:00
|
|
|
* @group Database
|
2013-05-06 22:34:50 +00:00
|
|
|
*/
|
|
|
|
public function testOnWikiList() {
|
|
|
|
$this->editPage( 'User:Foo/Bar-baz', "abc\ndef\r\nghi\n\n\n" );
|
|
|
|
|
2022-11-13 06:43:40 +00:00
|
|
|
$list = new OnWikiList( NS_USER, "Foo/Bar-baz" );
|
2013-05-06 22:34:50 +00:00
|
|
|
$this->assertEquals(
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 'abc', 'def', 'ghi' ],
|
2013-05-06 22:34:50 +00:00
|
|
|
$list->getValues()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testOnWikiListNonExistant() {
|
2022-11-13 06:43:40 +00:00
|
|
|
$list = new OnWikiList( NS_USER, "Some_Non_Existant_Page" );
|
2016-12-05 18:51:07 +00:00
|
|
|
$this->assertEquals( [], $list->getValues() );
|
2013-05-06 22:34:50 +00:00
|
|
|
}
|
|
|
|
}
|