Fix 'Tags' padding to keep it farther from the edge and document the source of the...
[lhc/web/wiklou.git] / tests / phpunit / includes / Storage / MutableRevisionRecordTest.php
1 <?php
2
3 namespace MediaWiki\Tests\Storage;
4
5 use CommentStoreComment;
6 use InvalidArgumentException;
7 use MediaWiki\Storage\MutableRevisionRecord;
8 use MediaWiki\Storage\RevisionAccessException;
9 use MediaWiki\Storage\RevisionRecord;
10 use MediaWiki\Storage\SlotRecord;
11 use MediaWiki\User\UserIdentityValue;
12 use MediaWikiTestCase;
13 use TextContent;
14 use Title;
15 use WikitextContent;
16
17 /**
18 * @covers \MediaWiki\Storage\MutableRevisionRecord
19 * @covers \MediaWiki\Storage\RevisionRecord
20 */
21 class MutableRevisionRecordTest extends MediaWikiTestCase {
22
23 use RevisionRecordTests;
24
25 /**
26 * @param array $rowOverrides
27 *
28 * @return MutableRevisionRecord
29 */
30 protected function newRevision( array $rowOverrides = [] ) {
31 $title = Title::newFromText( 'Dummy' );
32 $title->resetArticleID( 17 );
33
34 $user = new UserIdentityValue( 11, 'Tester', 0 );
35 $comment = CommentStoreComment::newUnsavedComment( 'Hello World' );
36
37 $record = new MutableRevisionRecord( $title );
38
39 if ( isset( $rowOverrides['rev_deleted'] ) ) {
40 $record->setVisibility( $rowOverrides['rev_deleted'] );
41 }
42
43 if ( isset( $rowOverrides['rev_id'] ) ) {
44 $record->setId( $rowOverrides['rev_id'] );
45 }
46
47 if ( isset( $rowOverrides['rev_page'] ) ) {
48 $record->setPageId( $rowOverrides['rev_page'] );
49 }
50
51 $record->setContent( 'main', new TextContent( 'Lorem Ipsum' ) );
52 $record->setComment( $comment );
53 $record->setUser( $user );
54
55 return $record;
56 }
57
58 public function provideConstructor() {
59 $title = Title::newFromText( 'Dummy' );
60 $title->resetArticleID( 17 );
61
62 yield [
63 $title,
64 'acmewiki'
65 ];
66 }
67
68 /**
69 * @dataProvider provideConstructor
70 *
71 * @param Title $title
72 * @param bool $wikiId
73 */
74 public function testConstructorAndGetters(
75 Title $title,
76 $wikiId = false
77 ) {
78 $rec = new MutableRevisionRecord( $title, $wikiId );
79
80 $this->assertSame( $title, $rec->getPageAsLinkTarget(), 'getPageAsLinkTarget' );
81 $this->assertSame( $wikiId, $rec->getWikiId(), 'getWikiId' );
82 }
83
84 public function provideConstructorFailure() {
85 $title = Title::newFromText( 'Dummy' );
86 $title->resetArticleID( 17 );
87
88 yield 'not a wiki id' => [
89 $title,
90 null
91 ];
92 }
93
94 /**
95 * @dataProvider provideConstructorFailure
96 *
97 * @param Title $title
98 * @param bool $wikiId
99 */
100 public function testConstructorFailure(
101 Title $title,
102 $wikiId = false
103 ) {
104 $this->setExpectedException( InvalidArgumentException::class );
105 new MutableRevisionRecord( $title, $wikiId );
106 }
107
108 public function testSetGetId() {
109 $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
110 $this->assertNull( $record->getId() );
111 $record->setId( 888 );
112 $this->assertSame( 888, $record->getId() );
113 }
114
115 public function testSetGetUser() {
116 $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
117 $user = $this->getTestSysop()->getUser();
118 $this->assertNull( $record->getUser() );
119 $record->setUser( $user );
120 $this->assertSame( $user, $record->getUser() );
121 }
122
123 public function testSetGetPageId() {
124 $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
125 $this->assertSame( 0, $record->getPageId() );
126 $record->setPageId( 999 );
127 $this->assertSame( 999, $record->getPageId() );
128 }
129
130 public function testSetGetParentId() {
131 $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
132 $this->assertNull( $record->getParentId() );
133 $record->setParentId( 100 );
134 $this->assertSame( 100, $record->getParentId() );
135 }
136
137 public function testGetMainContentWhenEmpty() {
138 $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
139 $this->setExpectedException( RevisionAccessException::class );
140 $this->assertNull( $record->getContent( 'main' ) );
141 }
142
143 public function testSetGetMainContent() {
144 $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
145 $content = new WikitextContent( 'Badger' );
146 $record->setContent( 'main', $content );
147 $this->assertSame( $content, $record->getContent( 'main' ) );
148 }
149
150 public function testGetSlotWhenEmpty() {
151 $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
152 $this->assertFalse( $record->hasSlot( 'main' ) );
153
154 $this->setExpectedException( RevisionAccessException::class );
155 $record->getSlot( 'main' );
156 }
157
158 public function testSetGetSlot() {
159 $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
160 $slot = SlotRecord::newUnsaved(
161 'main',
162 new WikitextContent( 'x' )
163 );
164 $record->setSlot( $slot );
165 $this->assertTrue( $record->hasSlot( 'main' ) );
166 $this->assertSame( $slot, $record->getSlot( 'main' ) );
167 }
168
169 public function testSetGetMinor() {
170 $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
171 $this->assertFalse( $record->isMinor() );
172 $record->setMinorEdit( true );
173 $this->assertSame( true, $record->isMinor() );
174 }
175
176 public function testSetGetTimestamp() {
177 $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
178 $this->assertNull( $record->getTimestamp() );
179 $record->setTimestamp( '20180101010101' );
180 $this->assertSame( '20180101010101', $record->getTimestamp() );
181 }
182
183 public function testSetGetVisibility() {
184 $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
185 $this->assertSame( 0, $record->getVisibility() );
186 $record->setVisibility( RevisionRecord::DELETED_USER );
187 $this->assertSame( RevisionRecord::DELETED_USER, $record->getVisibility() );
188 }
189
190 public function testSetGetSha1() {
191 $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
192 $this->assertSame( 'phoiac9h4m842xq45sp7s6u21eteeq1', $record->getSha1() );
193 $record->setSha1( 'someHash' );
194 $this->assertSame( 'someHash', $record->getSha1() );
195 }
196
197 public function testSetGetSize() {
198 $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
199 $this->assertSame( 0, $record->getSize() );
200 $record->setSize( 775 );
201 $this->assertSame( 775, $record->getSize() );
202 }
203
204 public function testSetGetComment() {
205 $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
206 $comment = new CommentStoreComment( 1, 'foo' );
207 $this->assertNull( $record->getComment() );
208 $record->setComment( $comment );
209 $this->assertSame( $comment, $record->getComment() );
210 }
211
212 }