summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/ModernTimeline/tests/Unit/JsonBuilderTest.php
blob: ba911187d0ff896ab198075f6874b5495fcd557c (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
151
152
153
154
155
156
157
158
159
160
161
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']
		);
	}

}