Debian Codegraph @

tests/phpunit/unit/languages/MessagesStructureTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Language\Language;
4
5 /**
6 * Validate the Messages*.php files
7 * @coversNothing -- no way to cover non-class files
8 */
9 class MessagesStructureTest extends MediaWikiUnitTestCase {
10 /** @var string */
11 private $langCode;
12 /** @var array */
13 private static $enData;
14
15 public static function provideMessagesFiles() {
16 $n = 0;
17 foreach ( glob( MW_INSTALL_PATH . '/languages/messages/Messages*.php' ) as $path ) {
18 $fileName = basename( $path );
19 yield [ $fileName ];
20 $n++;
21 }
22 if ( $n === 0 ) {
23 throw new \UnderflowException( 'Not enough files' );
24 }
25 }
26
27 /**
28 * @dataProvider provideMessagesFiles
29 * @param string $fileName
30 */
31 public function testMessageFile( $fileName ) {
32 $this->langCode = Language::getCodeFromFileName( $fileName, 'Messages' );
33
34 // Like isValidBuiltInCode()
35 $this->assertMatchesRegularExpression( '/^[a-z0-9-]{2,}$/', $this->langCode );
36
37 // Check for unrecognised variable names
38 $path = MW_INSTALL_PATH . '/languages/messages/' . $fileName;
39 $vars = $this->readFile( $path );
40 $unknownVars = array_diff(
41 array_keys( $vars ),
42 LocalisationCache::ALL_KEYS
43 );
44 $this->assertSame( [], $unknownVars, 'unknown variables' );
45
46 foreach ( $vars as $name => $value ) {
47 $method = 'validate' . ucfirst( $name );
48 if ( method_exists( $this, $method ) ) {
49 $this->$method( $value );
50 }
51 }
52 }
53
54 private function readFile( $_path ) {
55 require $_path;
56 $vars = get_defined_vars();
57 unset( $vars['_path'] );
58 return $vars;
59 }
60
61 private function getEnData() {
62 if ( self::$enData === null ) {
63 self::$enData = $this->readFile( MW_INSTALL_PATH . '/languages/messages/MessagesEn.php' );
64 }
65 return self::$enData;
66 }
67
68 private function validateFallback( $value ) {
69 if ( $this->langCode === 'en' ) {
70 $this->assertFalse( $value, 'fallback for en must be false' );
71 return;
72 }
73 $fallbacks = array_map( 'trim', explode( ',', $value ) );
74 $this->assertLessThanOrEqual(
75 MessageCache::MAX_REQUEST_LANGUAGES - 2,
76 count( $fallbacks ),
77 'fallback chain is too long (T310532)'
78 );
79 foreach ( $fallbacks as $code ) {
80 // Like isValidBuiltInCode()
81 $this->assertMatchesRegularExpression( '/^[a-z0-9-]{2,}$/', $code );
82 }
83 }
84
85 private function validateNamespaceNames( $names ) {
86 $enNames = $this->getEnData()['namespaceNames'];
87 $this->assertSame(
88 [],
89 array_diff( array_keys( $names ), array_keys( $enNames ) ),
90 'unrecognised namespace IDs'
91 );
92 foreach ( $names as $id => $name ) {
93 $this->assertIsString( $name );
94 if ( $id !== NS_MAIN ) {
95 $this->assertNotSame( '', $name );
96 }
97 $this->assertStringNotContainsString( ' ', $name, 'Use underscores in namespace names' );
98 }
99 }
100
101 private function validateNamespaceAliases( $aliases ) {
102 foreach ( $aliases as $alias => $id ) {
103 $this->assertIsString( $alias );
104 $this->assertNotSame( '', $alias );
105 $this->assertStringNotContainsString( ' ', $alias, 'Use underscores in namespace aliases' );
106 }
107 }
108
109 private function validateMagicWords( $magicWords ) {
110 $enWords = $this->getEnData()['magicWords'];
111 $this->assertSame(
112 [],
113 array_diff( array_keys( $magicWords ), array_keys( $enWords ) ),
114 'unrecognised magic word IDs'
115 );
116 foreach ( $magicWords as $id => $parts ) {
117 // Ideally the case should be an integer, but some script writes it as a string
118 $case = $parts[0];
119 $this->assertThat(
120 [ 0, 1, '0', '1' ],
121 $this->containsIdentical( $case ),
122 "$id case should be 0 or 1"
123 );
124 array_shift( $parts );
125 foreach ( $parts as $i => $syn ) {
126 $this->assertIsString( $syn, "$id syn $i should be string" );
127 $this->assertNotSame( '', $syn, "$id syn $i should not be empty" );
128 }
129 $canonical = $enWords[$id][1];
130 $this->assertContains( $canonical, $parts,
131 "$id should contain English synonym $canonical" );
132 }
133 }
134
135 private function validateSpecialPageAliases( $pages ) {
136 $enPages = $this->getEnData()['specialPageAliases'];
137 $this->assertSame(
138 [],
139 array_diff( array_keys( $pages ), array_keys( $enPages ) ),
140 'unrecognised special page IDs'
141 );
142 foreach ( $pages as $pageName => $aliases ) {
143 foreach ( $aliases as $i => $alias ) {
144 $this->assertIsString( $alias, "$pageName alias $i should be string" );
145 $this->assertNotSame( '', $alias,
146 "$pageName alias $i should not be empty" );
147 $this->assertStringNotContainsString( ' ', $alias, 'Use underscores in specialpage alias' );
148 }
149 }
150 }
151
152 private function validateLinkTrail( $linkTrail ) {
153 $this->assertIsString( $linkTrail );
154 $result = preg_match( $linkTrail, 'test' );
155 if ( $result === false ) {
156 $this->fail( "linkTrail regex match failed with code " . preg_last_error() );
157 }
158 }
159
160 }