2024-03-04 13:51:13 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Cite\Tests\Unit;
|
|
|
|
|
|
|
|
use Cite\Cite;
|
|
|
|
use Cite\Parsoid\ReferencesData;
|
2024-03-05 15:54:19 +00:00
|
|
|
use Cite\Parsoid\RefGroupItem;
|
2024-03-04 13:51:13 +00:00
|
|
|
use MediaWikiUnitTestCase;
|
|
|
|
use Wikimedia\Parsoid\Ext\ParsoidExtensionAPI;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Cite\Parsoid\ReferencesData
|
|
|
|
* @license GPL-2.0-or-later
|
|
|
|
*/
|
|
|
|
class ReferencesDataTest extends MediaWikiUnitTestCase {
|
|
|
|
|
|
|
|
public function testMinimalSetup() {
|
|
|
|
$data = new ReferencesData();
|
|
|
|
$this->assertSame( [], $data->embeddedErrors );
|
|
|
|
$this->assertSame( Cite::DEFAULT_GROUP, $data->referencesGroup );
|
|
|
|
$this->assertFalse( $data->inReferencesContent() );
|
|
|
|
$this->assertFalse( $data->inEmbeddedContent() );
|
|
|
|
$this->assertNull( $data->getRefGroup( Cite::DEFAULT_GROUP ) );
|
|
|
|
$this->assertSame( [], $data->getRefGroups() );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAllocIfMissing() {
|
|
|
|
$data = new ReferencesData();
|
|
|
|
$group = $data->getRefGroup( 'note', true );
|
|
|
|
$this->assertSame( 'note', $group->name );
|
|
|
|
$data->removeRefGroup( 'note' );
|
|
|
|
$this->assertNull( $data->getRefGroup( 'note' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEmbeddedInAnyContent() {
|
|
|
|
$data = new ReferencesData();
|
|
|
|
$data->pushEmbeddedContentFlag();
|
|
|
|
$this->assertTrue( $data->inEmbeddedContent() );
|
|
|
|
$this->assertFalse( $data->inReferencesContent() );
|
|
|
|
$data->popEmbeddedContentFlag();
|
|
|
|
$this->assertFalse( $data->inEmbeddedContent() );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEmbeddedInReferencesContent() {
|
|
|
|
$data = new ReferencesData();
|
|
|
|
$data->pushEmbeddedContentFlag( 'references' );
|
|
|
|
$this->assertTrue( $data->inEmbeddedContent() );
|
|
|
|
$this->assertTrue( $data->inReferencesContent() );
|
|
|
|
$data->popEmbeddedContentFlag();
|
|
|
|
$this->assertFalse( $data->inReferencesContent() );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddUnnamedRef() {
|
|
|
|
$data = new ReferencesData();
|
|
|
|
$ref = $data->add(
|
|
|
|
$this->createNoOpMock( ParsoidExtensionAPI::class ),
|
|
|
|
Cite::DEFAULT_GROUP,
|
|
|
|
'',
|
|
|
|
''
|
|
|
|
);
|
|
|
|
|
2024-03-05 15:54:19 +00:00
|
|
|
$expected = new RefGroupItem();
|
|
|
|
$expected->key = 'cite_ref-1';
|
|
|
|
$expected->id = 'cite_ref-1';
|
|
|
|
$expected->target = 'cite_note-1';
|
|
|
|
$this->assertEquals( $expected, $ref );
|
2024-03-04 13:51:13 +00:00
|
|
|
|
|
|
|
$group = $data->getRefGroup( Cite::DEFAULT_GROUP );
|
2024-03-05 15:54:19 +00:00
|
|
|
$this->assertEquals( [ $expected ], $group->refs );
|
2024-03-04 13:51:13 +00:00
|
|
|
$this->assertSame( [], $group->indexByName );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddNamedRef() {
|
|
|
|
$data = new ReferencesData();
|
|
|
|
$ref = $data->add(
|
|
|
|
$this->createNoOpMock( ParsoidExtensionAPI::class ),
|
|
|
|
'note',
|
|
|
|
'wales',
|
|
|
|
'rtl'
|
|
|
|
);
|
|
|
|
|
2024-03-05 15:54:19 +00:00
|
|
|
$expected = new RefGroupItem();
|
|
|
|
$expected->dir = 'rtl';
|
|
|
|
$expected->group = 'note';
|
|
|
|
$expected->key = 'cite_ref-wales_1';
|
|
|
|
$expected->id = 'cite_ref-wales_1-0';
|
|
|
|
$expected->name = 'wales';
|
|
|
|
$expected->target = 'cite_note-wales-1';
|
|
|
|
$this->assertEquals( $expected, $ref );
|
2024-03-04 13:51:13 +00:00
|
|
|
|
|
|
|
$group = $data->getRefGroup( 'note' );
|
2024-03-05 15:54:19 +00:00
|
|
|
$this->assertEquals( [ $expected ], $group->refs );
|
|
|
|
$this->assertEquals( [ 'wales' => $expected ], $group->indexByName );
|
2024-03-04 13:51:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|