summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/ModernTimeline/tests/Integration/OptionsTest.php
blob: d5352632d99c5e2b5a1a72394cd4e0dde68e957a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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']
		);
	}

}