Debian Codegraph @

extensions/Echo/tests/phpunit/unit/DiffParserTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Notifications\Test\Unit;
4
5 use MediaWiki\Extension\Notifications\DiscussionParser;
6 use MediaWikiUnitTestCase;
7
8 /**
9 * @covers \MediaWiki\Extension\Notifications\DiffParser
10 * @group Echo
11 */
12 class DiffParserTest extends MediaWikiUnitTestCase {
13
14 /**
15 * @dataProvider provider_getChangeSet
16 */
17 public function testGetChangeSet( $message, array $expect, $leftText, $rightText ) {
18 $changeSet = DiscussionParser::getMachineReadableDiff( $leftText, $rightText );
19 unset( $changeSet['_info'] );
20 $this->assertEquals( $expect, $changeSet, $message );
21 }
22
23 public static function provider_getChangeSet() {
24 return [
25
26 [
27 'Duplicate content must generate no changes',
28 // Expected change set
29 [],
30 // Left text
31 "a\nb\nc",
32 // Right text
33 "a\nb\nc",
34 ],
35
36 [
37 'Removing blank lines must generate no changes',
38 // Expected change set
39 [],
40 // Left text
41 "a\n\nb\n\nc",
42 // Right text
43 "a\nb\nc\n",
44 ],
45
46 [
47 'Must generate a single add change with only lines added',
48 // Expected change set
49 [ self::mockAction( 'add', "foo\nbar", 1 ) ],
50 // Left
51 "something",
52 // Right
53 "foo\nbar\nsomething",
54 ],
55
56 [
57 'Must generate a single subtract change with only lines subtracted',
58 // Expected change set
59 [ self::mockAction( 'subtract', "Zomg\nHiHiHi", 2 ) ],
60 // Left
61 "dummy\nZomg\nHiHiHi",
62 // Right
63 "dummy",
64 ],
65
66 [
67 'Adding content separated by no change must generate multiple changes',
68 // Expected change set
69 [
70 self::mockAction( 'add', 'b1', 3 ),
71 self::mockAction( 'add', "d1\nd2", 5, 6 ),
72 ],
73 // Left text
74 "a\nb\nc\nd\ne\nf",
75 // Right text
76 "a\nb\nb1\nc\nd\nd1\nd2\ne\nf",
77 ],
78
79 [
80 'Additon with empty line should be one action',
81 // Expected change set
82 [ self::mockAction( 'add', "User\n\nSignature", 1 ) ],
83 // Left text
84 "",
85 // Right text
86 "User\n\nSignature",
87 ],
88
89 [
90 'Extra blank lines on the edges must be trimmed',
91 // Expected change set
92 [ self::mockAction( 'add', "Zomg\nHiHiHi", 1 ) ],
93 // Left text
94 "",
95 // Right text
96 "\nZomg\nHiHiHi\n",
97 ],
98
99 [
100 'Extra blank lines inside the content must not be trimmed',
101 // Expected change set
102 [ self::mockAction( 'add', "\nZomg\nHiHiHi\n", 2 ) ],
103 // Left text
104 "foo\nbar",
105 // Right text
106 "foo\n\nZomg\nHiHiHi\n\nbar",
107 ],
108
109 [
110 'A blank line replaced with content must be an add',
111 // Expected change set
112 [ self::mockAction( 'add', 'cowbell', 1 ) ],
113 // Left Text
114 "",
115 // Right Text
116 "cowbell",
117 ],
118
119 [
120 'A blanked out line must be a subtraction',
121 // Expected change set
122 [ self::mockAction( 'subtract', 'cowbell', 1 ) ],
123 // Left text
124 "cowbell",
125 // Right text
126 "",
127 ],
128
129 [
130 'A line with its content replaced must be a change',
131 // Expected change set
132 [ self::mockChange( 'Its all about the journey', 'dummy', 1 ) ],
133 // Left text
134 "Its all about the journey",
135 // Right text
136 "dummy",
137 ],
138
139 [
140 'Changing lines and adding more must result in two changes',
141 // Expected change set
142 [
143 self::mockChange( 'Must be in a hurry to finish this thing', 'Must be in a hurry', 1 ),
144 self::mockAction( 'add', 'Finish this thing', 2 ),
145 ],
146 // Left text
147 "Must be in a hurry to finish this thing",
148 // Right text
149 "Must be in a hurry\nFinish this thing",
150 ],
151
152 [
153 'Changing multiple lines and adding more must result in two changes',
154 // Expected change set
155 [
156 self::mockChange( "Must not be\nin much of a hurry", "Must be\nin a hurry", 2 ),
157 self::mockAction( 'subtract', "to finish\nthis thing", 4 ),
158 ],
159 // Left text
160 "abc\nMust not be\nin much of a hurry\nto finish\nthis thing",
161 // Right text
162 "abc\nMust be\nin a hurry",
163 ],
164
165 [
166 'Must generate multiple add, change, and subtract actions',
167 // Expected change set
168 [
169 self::mockChange( "abc\nSome", "Other\nThings", 1 ),
170 self::mockAction( 'subtract', "Stuff", 3 ),
171 self::mockChange( "And\nThen", "There\nWas", 6, 5 ),
172 self::mockAction( 'add', 'Fencing', 8, 7 ),
173 ],
174 // Left text
175 "abc\nSome\nStuff\ndef\nghi\nAnd\nThen\njkl\nmno",
176 // Right text
177 "\nOther\nThings\ndef\nghi\nThere\nWas\nFencing\njkl\nmno",
178 ],
179 ];
180 }
181
182 protected static function mockAction( $action, $content, $left, $right = null ) {
183 return [
184 'action' => $action,
185 'content' => $content,
186 'left-pos' => $left,
187 'right-pos' => $right ?? $left,
188 ];
189 }
190
191 public static function mockChange( $oldContent, $newContent, $left, $right = null ) {
192 return [
193 'action' => 'change',
194 'old_content' => $oldContent,
195 'new_content' => $newContent,
196 'left-pos' => $left,
197 'right-pos' => $right ?? $left,
198 ];
199 }
200 }