xhprof: Guard against division by 0 when computing percentages
[lhc/web/wiklou.git] / tests / phpunit / includes / cache / LocalisationCacheTest.php
1 <?php
2 /**
3 * @group Database
4 * @group Cache
5 * @covers LocalisationCache
6 * @author Niklas Laxström
7 */
8 class LocalisationCacheTest extends MediaWikiTestCase {
9 protected function setUp() {
10 global $IP;
11
12 parent::setUp();
13 $this->setMwGlobals( array(
14 'wgExtensionMessagesFiles' => array(),
15 'wgHooks' => array(),
16 ) );
17 }
18
19 /**
20 * @return PHPUnit_Framework_MockObject_MockObject|LocalisationCache
21 */
22 protected function getMockLocalisationCache() {
23 global $IP;
24 $lc = $this->getMockBuilder( 'LocalisationCache' )
25 ->setConstructorArgs( array( array( 'store' => 'detect' ) ) )
26 ->setMethods( array( 'getMessagesDirs' ) )
27 ->getMock();
28 $lc->expects( $this->any() )->method( 'getMessagesDirs' )
29 ->will( $this->returnValue(
30 array( "$IP/tests/phpunit/data/localisationcache" )
31 ) );
32
33 return $lc;
34 }
35
36 public function testPuralRulesFallback() {
37 $cache = $this->getMockLocalisationCache();
38
39 $this->assertEquals(
40 $cache->getItem( 'ar', 'pluralRules' ),
41 $cache->getItem( 'arz', 'pluralRules' ),
42 'arz plural rules (undefined) fallback to ar (defined)'
43 );
44
45 $this->assertEquals(
46 $cache->getItem( 'ar', 'compiledPluralRules' ),
47 $cache->getItem( 'arz', 'compiledPluralRules' ),
48 'arz compiled plural rules (undefined) fallback to ar (defined)'
49 );
50
51 $this->assertNotEquals(
52 $cache->getItem( 'ksh', 'pluralRules' ),
53 $cache->getItem( 'de', 'pluralRules' ),
54 'ksh plural rules (defined) dont fallback to de (defined)'
55 );
56
57 $this->assertNotEquals(
58 $cache->getItem( 'ksh', 'compiledPluralRules' ),
59 $cache->getItem( 'de', 'compiledPluralRules' ),
60 'ksh compiled plural rules (defined) dont fallback to de (defined)'
61 );
62 }
63
64 public function testRecacheFallbacks() {
65 $lc = $this->getMockLocalisationCache();
66 $lc->recache( 'uk' );
67 $this->assertEquals(
68 array(
69 'present-uk' => 'uk',
70 'present-ru' => 'ru',
71 'present-en' => 'en',
72 ),
73 $lc->getItem( 'uk', 'messages' ),
74 'Fallbacks are only used to fill missing data'
75 );
76 }
77
78 public function testRecacheFallbacksWithHooks() {
79 // Use hook to provide updates for messages. This is what the
80 // LocalisationUpdate extension does. See bug 68781.
81 $this->mergeMwGlobalArrayValue( 'wgHooks', array(
82 'LocalisationCacheRecacheFallback' => array(
83 function (
84 LocalisationCache $lc,
85 $code,
86 array &$cache
87 ) {
88 if ( $code === 'ru' ) {
89 $cache['messages']['present-uk'] = 'ru-override';
90 $cache['messages']['present-ru'] = 'ru-override';
91 $cache['messages']['present-en'] = 'ru-override';
92 }
93 }
94 )
95 ) );
96
97 $lc = $this->getMockLocalisationCache();
98 $lc->recache( 'uk' );
99 $this->assertEquals(
100 array(
101 'present-uk' => 'uk',
102 'present-ru' => 'ru-override',
103 'present-en' => 'ru-override',
104 ),
105 $lc->getItem( 'uk', 'messages' ),
106 'Updates provided by hooks follow the normal fallback order.'
107 );
108 }
109 }