Skip to content

Issue 19 - call to undefined method WikiPage::factory() #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/IdProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ class IdProvider {
* @var callable Function to decide if string ID already exists, i.e. is already used as a
* WikiPage
*/
private $isUniqueId;
private $getUniqueIdChecker;

/**
* @param $generator
* @param null $isUniqueId
* @param null $getUniqueIdChecker
*/
public function __construct( $generator, $isUniqueId = null ) {
public function __construct( $generator, $getUniqueIdChecker = null ) {
$this->generator = $generator;
$this->isUniqueId = $isUniqueId;
$this->getUniqueIdChecker = $getUniqueIdChecker;
}

public function getId( array $params = [] ): string {
Expand All @@ -47,7 +47,7 @@ public function getId( array $params = [] ): string {
$prefix = trim( $prefix );
$id = $prefix . $this->generator->generate();
if ( !$skipUniqueTest ) {
while ( !( $this->isUniqueId )( $id ) ) {
while ( !( $this->getUniqueIdChecker )( $id ) ) {
$id = $prefix . $this->generator->generate();
}
}
Expand Down
16 changes: 12 additions & 4 deletions src/IdProviderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
}

private static function provider( $generator ) {
return new IdProvider( $generator, self::isUniqueId() );
return new IdProvider( $generator, self::getUniqueIdChecker() );
}

private static function dbExecute() {
Expand All @@ -59,13 +59,21 @@
}

/**
* Checks whether a WikiPage with the following id/title already exists
* Returns a closure that checks if a string ID or title is unique by verifying whether
* the corresponding WikiPage already exists. The closure can be invoked later with a specific
* title or ID to check for its uniqueness.
*
* @return \Closure
* @return \Closure A closure that takes a string $text as input and returns a boolean indicating
* whether the WikiPage associated with the given title/ID exists or not.
*/
private static function isUniqueId() {
private static function getUniqueIdChecker() {
return function ( $id ) {
$title = Title::newFromText( $id );

// If no Title object is found, the page does not exist
if ( $title === null ) {
return true;

Check warning on line 75 in src/IdProviderFactory.php

View check run for this annotation

Codecov / codecov/patch

src/IdProviderFactory.php#L75

Added line #L75 was not covered by tests
}

// MW 1.36+
if ( method_exists( MediaWikiServices::class, 'getWikiPageFactory' ) ) {
Expand Down
41 changes: 41 additions & 0 deletions tests/phpunit/Unit/IdProviderFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use MediaWiki\Extension\IdProvider\Generators\UuidGenerator;
use MediaWiki\Extension\IdProvider\IdProviderFactory;
use PHPUnit\Framework\TestCase;
use Title;

/**
* @group IDProvider
Expand Down Expand Up @@ -50,4 +51,44 @@ public function testIncrementCreatesIncrementIdGenerator() {
$provider = IdProviderFactory::increment();
$this->assertEquals( IncrementIdGenerator::class, $provider->generatorClass() );
}

public function testGetUniqueIdChecker() {
// Call the private method using Reflection
$reflection = new \ReflectionMethod( IdProviderFactory::class, 'getUniqueIdChecker' );
$reflection->setAccessible( true );
$isUniqueClosure = $reflection->invoke( null );

// check when id/title is from the page which exists and return false when page already exists
$this->assertFalse( $isUniqueClosure( 'Main Page' ) );

// check when id/title is from the page which not exists and return true if page do not exists
$this->assertTrue( $isUniqueClosure( 'Non existing page' ) );
}

public function testGetUniqueIdCheckerWhenTitleIsMalformed() {
// Call the private method using Reflection
$reflection = new \ReflectionMethod( IdProviderFactory::class, 'getUniqueIdChecker' );
$reflection->setAccessible( true );
$isUniqueClosure = $reflection->invoke( null );

$title = Title::newFromText( 'nonexisting title' );

// Asserts that an InvalidArgumentException is thrown and that message is good
$this->expectException( \InvalidArgumentException::class );
$this->expectExceptionMessage( '$text must be a string' );

// add object instead of string, it should thrown the InvalidArgumentException
$isUniqueClosure( $title );
}

public function testParamGet() {
// Call the private method using Reflection
$reflection = new \ReflectionMethod( IdProviderFactory::class, 'paramGet' );
$reflection->setAccessible( true );

$params = [ 'key' => 'value' ];

$this->assertEquals( 'value', $reflection->invoke( null, $params, 'key', 'default' ) );
$this->assertEquals( 'default', $reflection->invoke( null, $params, 'missing_key', 'default' ) );
}
}