diff options
author | Yaco <franco@reevo.org> | 2020-06-04 11:01:00 -0300 |
---|---|---|
committer | Yaco <franco@reevo.org> | 2020-06-04 11:01:00 -0300 |
commit | fc7369835258467bf97eb64f184b93691f9a9fd5 (patch) | |
tree | daabd60089d2dd76d9f5fb416b005fbe159c799d /www/wiki/extensions/ModernTimeline/tests |
first commit
Diffstat (limited to 'www/wiki/extensions/ModernTimeline/tests')
10 files changed, 696 insertions, 0 deletions
diff --git a/www/wiki/extensions/ModernTimeline/tests/Integration/OptionsTest.php b/www/wiki/extensions/ModernTimeline/tests/Integration/OptionsTest.php new file mode 100644 index 00000000..d5352632 --- /dev/null +++ b/www/wiki/extensions/ModernTimeline/tests/Integration/OptionsTest.php @@ -0,0 +1,150 @@ +<?php + +declare( strict_types = 1 ); + +namespace ModernTimeline\Tests\Integration; + +use ModernTimeline\TimelineOptions; +use ParamProcessor\ParamDefinitionFactory; +use ParamProcessor\ProcessingResult; +use ParamProcessor\Processor; +use PHPUnit\Framework\TestCase; + +/** + * @covers \ModernTimeline\TimelineOptions + */ +class OptionsTest extends TestCase { + + private const DEFAULT_SCALE_FACTOR = 2; + private const DEFAULT_START_SLIDE = 0; + + public function testDefaultOptions() { + $this->assertSame( + [ + 'hash_bookmark' => false, + 'default_bg_color' => 'white', + 'scale_factor' => self::DEFAULT_SCALE_FACTOR, + 'timenav_position' => 'bottom', + 'optimal_tick_width' => 100, + 'start_at_slide' => self::DEFAULT_START_SLIDE, + 'start_at_end' => false, + 'duration' => 1000, + 'timenav_height' => 200, + ], + $this->processUserInputToTimelineOptions( [] ) + ); + } + + private function processUserInputToTimelineOptions( array $userInput ): array { + return TimelineOptions::processedParamsToJson( + $this->processUserInput( $userInput )->getParameters() + ); + } + + private function processUserInput( array $userInput ): ProcessingResult { + $processor = Processor::newDefault(); + + $processor->setParameters( $userInput ); + $processor->setParameterDefinitions( $this->getParameterDefinitions() ); + + return $processor->processParameters(); + } + + private function getParameterDefinitions(): array { + return ParamDefinitionFactory::newDefault()->newDefinitionsFromArrays( + TimelineOptions::getTimelineParameterDefinitions() + ); + } + + public function testDefaultWidthAndHeight() { + $parameters = $this->processUserInput( [] )->getParameterArray(); + + $this->assertSame( $GLOBALS['wgModernTimelineWidth'], $parameters['width'] ); + $this->assertSame( $GLOBALS['wgModernTimelineHeight'], $parameters['height'] ); + } + + /** + * @dataProvider widthProvider + */ + public function testWidth( string $input, string $expected ) { + $parameters = $this->processUserInput( [ + 'width' => $input, + ] )->getParameterArray(); + + $this->assertSame( $expected, $parameters['width'] ); + } + + public function widthProvider() { + yield [ '10', '10px' ]; + yield [ '10px', '10px' ]; + yield [ '10%', '10%' ]; + yield [ '10em', '10em' ]; + yield [ '10ex', '10ex' ]; + yield [ 'auto', 'auto' ]; + } + + /** + * @dataProvider heightProvider + */ + public function testHeight( string $input, string $expected ) { + $parameters = $this->processUserInput( [ + 'height' => $input, + ] )->getParameterArray(); + + $this->assertSame( $expected, $parameters['height'] ); + } + + public function heightProvider() { + yield [ '10', '10px' ]; + yield [ '10px', '10px' ]; + yield [ '10em', '10em' ]; + yield [ '10ex', '10ex' ]; + } + + public function testTooLowScaleFactorDefaults() { + $this->assertProcesses( 'scale factor', '0', self::DEFAULT_SCALE_FACTOR ); + } + + private function assertProcesses( string $paramName, string $input, $expected ) { + $parameters = $this->processUserInput( [ + $paramName => $input, + ] )->getParameterArray(); + + $this->assertSame( $expected, $parameters[$paramName] ); + } + + public function testTooLowStartSlideDefaults() { + $this->assertProcesses( 'start slide', '0', 1 ); + } + + public function testStartSlideIsOneBased() { + $this->assertSame( + 2, + $this->processUserInputToTimelineOptions( [ 'start slide' => '3' ] )['start_at_slide'] + ); + } + + /** + * @dataProvider animationDurationAliasProvider + */ + public function testAnimationDurationAliases( string $alias ) { + $parameters = $this->processUserInput( [ + $alias => '42', + ] )->getParameterArray(); + + $this->assertSame( 42, $parameters['transition duration'] ); + } + + public function animationDurationAliasProvider() { + yield 'automatic alias' => [ 'transitionduration' ]; + yield 'manual alias' => [ 'duration' ]; + } + + public function testNavHeightUsesCorrectJsonFieldWhenGivenPercentage() { + $this->assertSame( + 50, + $this->processUserInputToTimelineOptions( [ 'navigation height' => '50%' ] )['timenav_height_percentage'] + ); + } + +} diff --git a/www/wiki/extensions/ModernTimeline/tests/System/JsonScript/escaping.json b/www/wiki/extensions/ModernTimeline/tests/System/JsonScript/escaping.json new file mode 100644 index 00000000..d26547d1 --- /dev/null +++ b/www/wiki/extensions/ModernTimeline/tests/System/JsonScript/escaping.json @@ -0,0 +1,51 @@ +{ + "description": "Modern Timeline - escaping", + "setup": [ + { + "page": "News date", + "namespace": "SMW_NS_PROPERTY", + "contents": "[[Has type::Date]]" + }, + { + "page": "Description", + "namespace": "SMW_NS_PROPERTY", + "contents": "[[Has type::Text]]" + }, + { + "page": "Evil page", + "contents": "[[News date::August 11, 2019]] [[Description::<script></script>]]" + }, + { + "page": "Query page", + "contents": "{{ #ask: [[Description::+]] | format=moderntimeline | ?News date | ?Description }}" + } + ], + "tests": [ + { + "about": "Evil descriptions are escaped", + "type": "parser", + "subject": "Query page", + "assert-output": { + "include-head-items": true, + "to-contain": [ + "Evil page", + "2019", + "Description: &lt;script&gt;&lt;\\/script&gt" + ] + } + } + ], + "settings": { + "wgContLang": "en", + "wgLang": "en", + "smwgNamespacesWithSemanticLinks": { + "NS_MAIN": true, + "SMW_NS_PROPERTY": true + } + }, + "meta": { + "version": "2", + "is-incomplete": false, + "debug": false + } +}
\ No newline at end of file diff --git a/www/wiki/extensions/ModernTimeline/tests/System/JsonScript/parameters.json b/www/wiki/extensions/ModernTimeline/tests/System/JsonScript/parameters.json new file mode 100644 index 00000000..a12a5fc5 --- /dev/null +++ b/www/wiki/extensions/ModernTimeline/tests/System/JsonScript/parameters.json @@ -0,0 +1,35 @@ +{ + "description": "Modern Timeline - parameters", + "setup": [ + { + "page": "Query with modified height", + "contents": "{{ #ask: [[News date::+]] | format=moderntimeline | ?News date | height=1337 }}" + } + ], + "tests": [ + { + "about": "Width and height parameters", + "type": "parser", + "subject": "Query with modified height", + "assert-output": { + "to-contain": [ + "width: 100%", + "height: 1337px" + ] + } + } + ], + "settings": { + "wgContLang": "en", + "wgLang": "en", + "smwgNamespacesWithSemanticLinks": { + "NS_MAIN": true, + "SMW_NS_PROPERTY": true + } + }, + "meta": { + "version": "2", + "is-incomplete": false, + "debug": false + } +}
\ No newline at end of file diff --git a/www/wiki/extensions/ModernTimeline/tests/System/JsonScript/query.json b/www/wiki/extensions/ModernTimeline/tests/System/JsonScript/query.json new file mode 100644 index 00000000..bca28efc --- /dev/null +++ b/www/wiki/extensions/ModernTimeline/tests/System/JsonScript/query.json @@ -0,0 +1,93 @@ +{ + "description": "Modern Timeline - query result content", + "setup": [ + { + "page": "News date", + "namespace": "SMW_NS_PROPERTY", + "contents": "[[Has type::Date]]" + }, + { + "page": "End date", + "namespace": "SMW_NS_PROPERTY", + "contents": "[[Has type::Date]]" + }, + { + "page": "Description", + "namespace": "SMW_NS_PROPERTY", + "contents": "[[Has type::Text]]" + }, + { + "page": "First news event", + "contents": "[[News date::August 1, 2019]] [[End date::August 3, 2019]] [[Description::first desc]]" + }, + { + "page": "Second news event", + "contents": "[[News date::August 6, 2019]] [[Description::second desc]]" + }, + { + "page": "Page without date", + "contents": "[[Description::No date here]]" + }, + { + "page": "Query page", + "contents": "{{ #ask: [[Description::+]] | format=moderntimeline | ?News date | ?End date | ?Description }}" + } + ], + "tests": [ + { + "about": "Only pages with dates are shown", + "type": "parser", + "subject": "Query page", + "assert-output": { + "include-head-items": true, + "to-contain": [ + "First news event", + "Second news event" + ], + "not-contain": [ + "No date", + "without date" + ] + } + }, + { + "about": "Dates are included", + "type": "parser", + "subject": "Query page", + "assert-output": { + "include-head-items": true, + "to-contain": [ + "month\":8", + "day\":1", + "day\":3", + "day\":6" + ] + } + }, + { + "about": "Extra printouts are included", + "type": "parser", + "subject": "Query page", + "assert-output": { + "include-head-items": true, + "to-contain": [ + "first desc", + "second desc" + ] + } + } + ], + "settings": { + "wgContLang": "en", + "wgLang": "en", + "smwgNamespacesWithSemanticLinks": { + "NS_MAIN": true, + "SMW_NS_PROPERTY": true + } + }, + "meta": { + "version": "2", + "is-incomplete": false, + "debug": false + } +}
\ No newline at end of file diff --git a/www/wiki/extensions/ModernTimeline/tests/System/JsonScript/smwdoc.json b/www/wiki/extensions/ModernTimeline/tests/System/JsonScript/smwdoc.json new file mode 100644 index 00000000..24333029 --- /dev/null +++ b/www/wiki/extensions/ModernTimeline/tests/System/JsonScript/smwdoc.json @@ -0,0 +1,37 @@ +{ + "description": "Modern Timeline - smwdoc", + "setup": [ + { + "page": "SMWDoc", + "contents": "{{#smwdoc:moderntimeline}}" + } + ], + "tests": [ + { + "about": "All parameters have a message", + "type": "parser", + "subject": "SMWDoc", + "assert-output": { + "to-contain": [ + "Background" + ], + "not-contain": [ + "modern-timeline-" + ] + } + } + ], + "settings": { + "wgContLang": "en", + "wgLang": "en", + "smwgNamespacesWithSemanticLinks": { + "NS_MAIN": true, + "SMW_NS_PROPERTY": true + } + }, + "meta": { + "version": "2", + "is-incomplete": false, + "debug": false + } +}
\ No newline at end of file diff --git a/www/wiki/extensions/ModernTimeline/tests/System/JsonScript/template.json b/www/wiki/extensions/ModernTimeline/tests/System/JsonScript/template.json new file mode 100644 index 00000000..99f883c9 --- /dev/null +++ b/www/wiki/extensions/ModernTimeline/tests/System/JsonScript/template.json @@ -0,0 +1,54 @@ +{ + "description": "Modern Timeline - template", + "setup": [ + { + "page": "News date", + "namespace": "SMW_NS_PROPERTY", + "contents": "[[Has type::Date]]" + }, + { + "page": "Description", + "namespace": "SMW_NS_PROPERTY", + "contents": "[[Has type::Text]]" + }, + { + "page": "TimelineTest", + "namespace": "NS_TEMPLATE", + "contents": "Hi! {{{title}}} {{{Description}}} {{{News date}}}" + }, + { + "page": "First news event", + "contents": "[[News date::August 1, 2019]] [[Description::first desc]]" + }, + { + "page": "Query page", + "contents": "{{ #ask: [[Description::+]] | format=moderntimeline | ?News date | ?Description | template=TimelineTest }}" + } + ], + "tests": [ + { + "about": "Template parameter", + "type": "parser", + "subject": "Query page", + "assert-output": { + "include-head-items": true, + "to-contain": [ + "Hi! First news event first desc 1 August 2019" + ] + } + } + ], + "settings": { + "wgContLang": "en", + "wgLang": "en", + "smwgNamespacesWithSemanticLinks": { + "NS_MAIN": true, + "SMW_NS_PROPERTY": true + } + }, + "meta": { + "version": "2", + "is-incomplete": false, + "debug": false + } +}
\ No newline at end of file diff --git a/www/wiki/extensions/ModernTimeline/tests/System/JsonScriptTest.php b/www/wiki/extensions/ModernTimeline/tests/System/JsonScriptTest.php new file mode 100644 index 00000000..4ee5aa4e --- /dev/null +++ b/www/wiki/extensions/ModernTimeline/tests/System/JsonScriptTest.php @@ -0,0 +1,15 @@ +<?php + +declare( strict_types = 1 ); + +namespace ModernTimeline\Tests\System; + +use SMW\Tests\Integration\JSONScript\JsonTestCaseScriptRunnerTest; + +class JsonScriptTest extends JsonTestCaseScriptRunnerTest { + + protected function getTestCaseLocation() { + return __DIR__ . '/JsonScript'; + } + +} diff --git a/www/wiki/extensions/ModernTimeline/tests/Unit/JsonBuilderTest.php b/www/wiki/extensions/ModernTimeline/tests/Unit/JsonBuilderTest.php new file mode 100644 index 00000000..ba911187 --- /dev/null +++ b/www/wiki/extensions/ModernTimeline/tests/Unit/JsonBuilderTest.php @@ -0,0 +1,162 @@ +<?php + +declare( strict_types = 1 ); + +namespace ModernTimeline\Tests\Unit; + +use ModernTimeline\JsonBuilder; +use ModernTimeline\ResultFacade\PropertyValueCollection; +use ModernTimeline\ResultFacade\Subject; +use ModernTimeline\ResultFacade\SubjectCollection; +use ModernTimeline\SlidePresenter\SimpleSlidePresenter; +use ModernTimeline\SlidePresenter\SlidePresenter; +use PHPUnit\Framework\TestCase; +use SMW\DIWikiPage; +use SMW\Query\PrintRequest; +use SMWDITime; + +/** + * @covers \ModernTimeline\JsonBuilder + */ +class JsonBuilderTest extends TestCase { + + private const PAGE_NAME = 'Some Page'; + + public function testEmptySubjectCollection() { + $this->assertBuildsJson( + [], + new SubjectCollection( [] ) + ); + } + + public function assertBuildsJson( array $expectedJson, SubjectCollection $input ) { + $this->assertSame( + [ + 'events' => $expectedJson + ], + $this->toJson( $input ) + ); + } + + private function toJson( SubjectCollection $input ): array { + return ( new JsonBuilder( new SimpleSlidePresenter() ) )->buildTimelineJson( $input ); + } + + public function testOnlySubjectsWithNoValues() { + $this->assertBuildsJson( + [], + new SubjectCollection( + [ + new Subject( + $this->newDiWikiPage(), + [] + ) + ] + ) + ); + } + + private function newDiWikiPage( string $pageName = self::PAGE_NAME ): DIWikiPage { + $page = $this->createMock( DIWikiPage::class ); + + $page->method( 'getTitle' )->willReturn( \Title::newFromText( $pageName ) ); + + return $page; + } + + private function newSinglePageWithStartAndEndDate(): SubjectCollection { + return new SubjectCollection( + [ + new Subject( + $this->newDiWikiPage(), + [ + new PropertyValueCollection( + $this->newDatePrintRequestWithLabel( 'Has date' ), + [ + new SMWDITime( + SMWDITime::CM_GREGORIAN, + 2019, + 8, + 2, + 16, + 7, + 42 + ) + ] + ), + new PropertyValueCollection( + $this->newDatePrintRequestWithLabel( 'End date' ), + [ + new SMWDITime( + SMWDITime::CM_GREGORIAN, + 2019, + 8, + 5, + 17, + 39, + 23 + ) + ] + ) + ] + ) + ] + ); + } + + private function newDatePrintRequestWithLabel( string $label ): PrintRequest { + $pr = $this->createMock( PrintRequest::class ); + $pr->method( 'getLabel' )->willReturn( $label ); + $pr->method( 'getTypeID' )->willReturn( '_dat' ); + return $pr; + } + + public function testStartDate() { + $json = $this->toJson( $this->newSinglePageWithStartAndEndDate() ); + + $this->assertSame( + [ + 'year' => 2019, + 'month' => 8, + 'day' => 2, + 'hour' => 16, + 'minute' => 7, + 'second' => 42, + ], + $json['events'][0]['start_date'] + ); + } + + public function testEndDate() { + $json = $this->toJson( $this->newSinglePageWithStartAndEndDate() ); + + $this->assertSame( + [ + 'year' => 2019, + 'month' => 8, + 'day' => 5, + 'hour' => 17, + 'minute' => 39, + 'second' => 23, + ], + $json['events'][0]['end_date'] + ); + } + + public function testHeadline() { + $json = $this->toJson( $this->newSinglePageWithStartAndEndDate() ); + + $this->assertContains( + self::PAGE_NAME, + $json['events'][0]['text']['headline'] + ); + } + + public function testPageWithStartAndEndDateOnlyLeadsToOneEvent() { + $this->assertCount( + 1, + $this->toJson( $this->newSinglePageWithStartAndEndDate() )['events'] + ); + } + +} diff --git a/www/wiki/extensions/ModernTimeline/tests/Unit/SlidePresenter/TemplateSlidePresenterTest.php b/www/wiki/extensions/ModernTimeline/tests/Unit/SlidePresenter/TemplateSlidePresenterTest.php new file mode 100644 index 00000000..64626df7 --- /dev/null +++ b/www/wiki/extensions/ModernTimeline/tests/Unit/SlidePresenter/TemplateSlidePresenterTest.php @@ -0,0 +1,81 @@ +<?php + +declare( strict_types = 1 ); + +namespace ModernTimeline\Tests\Unit\SlidePresenter; + +use ModernTimeline\ResultFacade\PropertyValueCollection; +use ModernTimeline\ResultFacade\Subject; +use ModernTimeline\SlidePresenter\TemplateSlidePresenter; +use PHPUnit\Framework\TestCase; +use SMW\DIWikiPage; +use SMW\Query\PrintRequest; +use SMWDITime; + +/** + * @covers \ModernTimeline\SlidePresenter\TemplateSlidePresenter + */ +class TemplateSlidePresenterTest extends TestCase { + + private const PAGE_NAME = 'Some Page'; + + public function testTemplate() { + $this->assertSame( + '{{TemplateName|title=Some Page|Has date=2 August 2019 16:07:42|End date=5 August 2019 17:39:23}}', + ( new TemplateSlidePresenter( 'TemplateName' ) )->getTemplateText( $this->newSinglePageWithStartAndEndDate() ) + ); + } + + private function newSinglePageWithStartAndEndDate(): Subject { + return new Subject( + $this->newDiWikiPage(), + [ + new PropertyValueCollection( + $this->newDatePrintRequestWithLabel( 'Has date' ), + [ + new SMWDITime( + SMWDITime::CM_GREGORIAN, + 2019, + 8, + 2, + 16, + 7, + 42 + ) + ] + ), + new PropertyValueCollection( + $this->newDatePrintRequestWithLabel( 'End date' ), + [ + new SMWDITime( + SMWDITime::CM_GREGORIAN, + 2019, + 8, + 5, + 17, + 39, + 23 + ) + ] + ) + ] + ); + } + + private function newDiWikiPage(): DIWikiPage { + $page = $this->createMock( DIWikiPage::class ); + + $page->method( 'getTitle' )->willReturn( \Title::newFromText( self::PAGE_NAME ) ); + + return $page; + } + + private function newDatePrintRequestWithLabel( string $label ): PrintRequest { + $pr = $this->createMock( PrintRequest::class ); + $pr->method( 'getLabel' )->willReturn( $label ); + $pr->method( 'getText' )->willReturn( $label ); + $pr->method( 'getTypeID' )->willReturn( '_dat' ); + return $pr; + } + +} diff --git a/www/wiki/extensions/ModernTimeline/tests/bootstrap.php b/www/wiki/extensions/ModernTimeline/tests/bootstrap.php new file mode 100644 index 00000000..1df89fdf --- /dev/null +++ b/www/wiki/extensions/ModernTimeline/tests/bootstrap.php @@ -0,0 +1,18 @@ +<?php + +declare( strict_types = 1 ); + +if ( PHP_SAPI !== 'cli' ) { + die( 'Not an entry point' ); +} + +error_reporting( -1 ); +ini_set( 'display_errors', '1' ); + +if ( !is_readable( $autoloaderClassPath = __DIR__ . '/../../SemanticMediaWiki/tests/autoloader.php' ) ) { + die( "\nThe Semantic MediaWiki test autoloader is not available" ); +} + +require $autoloaderClassPath; + +$autoloader->addPsr4( 'SMW\\Tests\\', __DIR__ . '/../../SemanticMediaWiki/tests/phpunit' ); |