testEnvironment = new TestEnvironment(); $this->dataItemFactory = new DataItemFactory(); $this->propertySpecificationLookup = $this->getMockBuilder( SpecificationLookup::class ) ->disableOriginalConstructor() ->getMock(); $constraintValueValidator = $this->getMockBuilder( '\SMW\DataValues\ValueValidators\ConstraintValueValidator' ) ->disableOriginalConstructor() ->getMock(); $externalFormatterUriValue = $this->getMockBuilder( '\SMW\DataValues\ExternalFormatterUriValue' ) ->disableOriginalConstructor() ->getMockForAbstractClass(); $this->dataValueServiceFactory = $this->getMockBuilder( '\SMW\Services\DataValueServiceFactory' ) ->disableOriginalConstructor() ->getMock(); $this->dataValueServiceFactory->expects( $this->any() ) ->method( 'getPropertySpecificationLookup' ) ->willReturn( $this->propertySpecificationLookup ); $this->dataValueServiceFactory->expects( $this->any() ) ->method( 'getConstraintValueValidator' ) ->willReturn( $constraintValueValidator ); $this->dataValueServiceFactory->expects( $this->any() ) ->method( 'getDataValueFactory' ) ->willReturn( DataValueFactory::getInstance() ); $this->testEnvironment->registerObject( 'PropertySpecificationLookup', $this->propertySpecificationLookup ); } protected function tearDown(): void { $this->testEnvironment->tearDown(); } public function testCanConstruct() { $this->assertInstanceOf( ExternalIdentifierValue::class, new ExternalIdentifierValue() ); } public function testGetShortWikiText() { $this->propertySpecificationLookup->expects( $this->once() ) ->method( 'getExternalFormatterUri' ) ->willReturn( $this->dataItemFactory->newDIUri( 'http', 'example.org/$1' ) ); $instance = new ExternalIdentifierValue(); $instance->setDataValueServiceFactory( $this->dataValueServiceFactory ); $instance->setUserValue( 'foo' ); $instance->setProperty( $this->dataItemFactory->newDIProperty( 'Bar' ) ); $this->assertEquals( 'foo', $instance->getShortWikiText() ); $this->assertEquals( '[http://example.org/foo foo]', $instance->getShortWikiText( 'linker' ) ); } public function testGetShortWikiText_Nowiki() { $this->propertySpecificationLookup->expects( $this->once() ) ->method( 'getExternalFormatterUri' ) ->willReturn( $this->dataItemFactory->newDIUri( 'http', 'example.org/$1' ) ); $instance = new ExternalIdentifierValue(); $instance->setDataValueServiceFactory( $this->dataValueServiceFactory ); $instance->setUserValue( 'foo' ); $instance->setOutputFormat( 'nowiki' ); $instance->setProperty( $this->dataItemFactory->newDIProperty( 'Bar' ) ); $this->assertEquals( 'foo', $instance->getShortWikiText() ); $this->assertEquals( 'http://example.org/foo', $instance->getShortWikiText( 'linker' ) ); } /** * @dataProvider identifierProvider */ public function testGetShortHTMLText( $value, $uri, $expected_text, $expected_html ) { $this->propertySpecificationLookup->expects( $this->once() ) ->method( 'getExternalFormatterUri' ) ->willReturn( $uri ); $instance = new ExternalIdentifierValue(); $instance->setDataValueServiceFactory( $this->dataValueServiceFactory ); $instance->setUserValue( $value ); $instance->setProperty( $this->dataItemFactory->newDIProperty( 'Bar' ) ); $this->assertEquals( $expected_text, $instance->getShortHTMLText() ); $this->assertEquals( $expected_html, $instance->getShortHTMLText( 'linker' ) ); $this->assertEmpty( $instance->getErrors() ); } public function testTryToGetShortHTMLTextWithLinkerOnMissingFormatterUri() { $instance = new ExternalIdentifierValue(); $instance->setDataValueServiceFactory( $this->dataValueServiceFactory ); $instance->setUserValue( 'foo' ); $instance->setProperty( $this->dataItemFactory->newDIProperty( 'Bar' ) ); $this->assertEquals( 'foo', $instance->getShortHTMLText() ); $instance->getShortHTMLText( 'linker' ); $this->assertNotEmpty( $instance->getErrors() ); } public function identifierProvider() { $dataItemFactory = new DataItemFactory(); yield [ 'foo', $dataItemFactory->newDIUri( 'http', 'example.org/$1' ), 'foo', 'foo' ]; // foo + 1001 [main,extra] yield [ 'foo{1001}', $dataItemFactory->newDIUri( 'http', 'example.org/$1&id=$2' ), 'foo{1001}', 'foo' ]; // trim space / main part and extra parameters yield [ 'foo {1001}', $dataItemFactory->newDIUri( 'http', 'example.org/$1&id=$2' ), 'foo {1001}', 'foo' ]; // encoded comma yield [ 'foo {1\,2\,and}', $dataItemFactory->newDIUri( 'http', 'example.org/$1&id=$2' ), 'foo {1,2,and}', 'foo' ]; // no multi substitute yield [ 'foo {1001}', $dataItemFactory->newDIUri( 'http', 'example.org/$1' ), 'foo {1001}', 'foo {1001}' ]; } }