test: refactor/speed up release note file test
[lhc/web/wiklou.git] / tests / phpunit / documentation / ReleaseNotesTest.php
1 <?php
2
3 /**
4 * James doesn't like having to manually fix these things.
5 */
6 class ReleaseNotesTest extends MediaWikiTestCase {
7 /**
8 * Verify that at least one Release Notes file exists, have content, and
9 * aren't overly long.
10 *
11 * @group documentation
12 * @coversNothing
13 */
14 public function testReleaseNotesFilesExistAndAreNotMalformed() {
15 global $wgVersion, $IP;
16
17 $notesFiles = glob( "$IP/RELEASE-NOTES-*" );
18
19 $this->assertGreaterThanOrEqual(
20 1,
21 count( $notesFiles ),
22 'Repo has at least one Release Notes file.'
23 );
24
25 $versionParts = explode( '.', explode( '-', $wgVersion )[0] );
26 $this->assertContains(
27 "$IP/RELEASE-NOTES-$versionParts[0].$versionParts[1]",
28 $notesFiles,
29 'Repo has a Release Notes file for the current $wgVersion.'
30 );
31
32 foreach ( $notesFiles as $index => $fileName ) {
33 $this->assertFileLength( "Release Notes", $fileName );
34 }
35 }
36
37 public static function provideFilesAtRoot() {
38 global $IP;
39
40 $rootFiles = [
41 "COPYING",
42 "FAQ",
43 "HISTORY",
44 "INSTALL",
45 "README",
46 "SECURITY",
47 ];
48
49 $testCases = [];
50 foreach ( $rootFiles as $rootFile ) {
51 $testCases["$rootFile file"] = [ "$IP/$rootFile" ];
52 }
53 return $testCases;
54 }
55
56 /**
57 * @dataProvider provideFilesAtRoot
58 * @coversNothing
59 */
60 public function testRootFilesHaveProperLineLength( $fileName ) {
61 $this->assertFileLength( "Help", $fileName );
62 }
63
64 private function assertFileLength( $type, $fileName ) {
65 $lines = file( $fileName, FILE_IGNORE_NEW_LINES );
66
67 $this->assertNotFalse(
68 $lines,
69 "$type file '$fileName' is inaccessible."
70 );
71
72 $errors = [];
73 foreach ( $lines as $i => $line ) {
74 $num = $i + 1;
75
76 // FILE_IGNORE_NEW_LINES drops the \n at the EOL, so max length is 80 not 81.
77 $max_length = 80;
78
79 $length = mb_strlen( $line );
80 if ( $length <= $max_length ) {
81 continue;
82 }
83 $errors[] = "line $num: length $length > $max_length:\n$line";
84 }
85 # Using assertSame() to show the full line
86 $this->assertSame(
87 [], $errors,
88 "$type file '$fileName' lines " .
89 "have at most $max_length characters"
90 );
91 }
92 }