summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Bootstrap/tests/phpunit/BootstrapManagerTest.php
blob: 5da607072b7b9d2332ca2ac386a53fa32b93ee12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php

namespace Bootstrap\Tests;

use Bootstrap\BootstrapManager;

/**
 * @uses \Bootstrap\BootstrapManager
 *
 * @ingroup Test
 *
 * @group extension-bootstrap
 * @group mediawiki-databaseless
 *
 * @license GNU GPL v3+
 * @since 1.0
 *
 * @author mwjames
 */
class BootstrapManagerTest extends \PHPUnit_Framework_TestCase {

	protected $wgResourceModules = null;

	protected function setUp() {
		parent::setUp();
		$this->wgResourceModules = $GLOBALS['wgResourceModules'];

		// Preset with empty default values to verify the initialization status
		// during invocation
		$GLOBALS['wgResourceModules'][ 'ext.bootstrap.styles' ] = array(
			'localBasePath'   => '',
			'remoteBasePath'  => '',
			'class'           => '',
			'dependencies'    => array(),
			'styles'          => array(),
			'variables'       => array(),
			'external styles' => array()
		);

		$GLOBALS['wgResourceModules'][ 'ext.bootstrap.scripts' ] = array(
			'dependencies'    => array(),
			'scripts'         => array()
		);
	}

	protected function tearDown() {
		$GLOBALS['wgResourceModules'] = $this->wgResourceModules;
		BootstrapManager::clear();

		parent::tearDown();
	}

	public function testCanConstruct() {

		$moduleDefinition = $this->getMockBuilder( '\Bootstrap\Definition\ModuleDefinition' )
			->disableOriginalConstructor()
			->setMethods( array( 'get' ) )
			->getMock();

		$moduleDefinition->expects( $this->atLeastOnce() )
			->method( 'get' )
			->will( $this->returnValue( array() ) );

		$this->assertInstanceOf(
			'\Bootstrap\BootstrapManager',
			new BootstrapManager( $moduleDefinition )
		);

		BootstrapManager::clear();

		$this->assertInstanceOf(
			'\Bootstrap\BootstrapManager',
			BootstrapManager::getInstance()
		);
	}

	public function testLoadStylesFromModuleDefinition() {

		$this->assertEmpty( $this->getGlobalResourceModuleBootstrapStyles() );

		$moduleDefinition = $this->getMockBuilder( '\Bootstrap\Definition\ModuleDefinition' )
			->disableOriginalConstructor()
			->setMethods( array( 'get' ) )
			->getMock();

		$moduleDefinition->expects( $this->at( 0 ) )
			->method( 'get' )
			->with( $this->stringContains( 'descriptions' ) )
			->will( $this->returnValue( array( 'variables' => array( 'styles' => 'variables' ) ) ) );

		$moduleDefinition->expects( $this->at( 1 ) )
			->method( 'get' )
			->with( $this->stringContains( 'core' ) )
			->will( $this->returnValue( array( 'variables' ) ) );

		$moduleDefinition->expects( $this->at( 2 ) )
			->method( 'get' )
			->with( $this->stringContains( 'optional' ) )
			->will( $this->returnValue( array( 'foo' ) ) );

		$instance = new BootstrapManager( $moduleDefinition );
		$instance->addAllBootstrapModules();

		$this->assertNotEmpty( $this->getGlobalResourceModuleBootstrapStyles() );
	}

	public function testSetLessVariables() {

		$moduleDefinition = $this->getMockBuilder( '\Bootstrap\Definition\ModuleDefinition' )
			->disableOriginalConstructor()
			->setMethods( array( 'get' ) )
			->getMock();

		$moduleDefinition->expects( $this->atLeastOnce() )
			->method( 'get' )
			->will( $this->returnValue( array() ) );

		$instance = new BootstrapManager( $moduleDefinition );
		$instance->setLessVariables( array( 'foo' => 'bar') );
		$instance->setLessVariable( 'ichi', 'ni' );

		$this->assertArrayHasKey(
			'foo',
			$this->getGlobalResourceModuleBootstrapVariables()
		);

		$this->assertArrayHasKey(
			'ichi',
			$this->getGlobalResourceModuleBootstrapVariables()
		);
	}

	public function testAddCacheTriggerFiles() {

		$moduleDefinition = $this->getMockBuilder( '\Bootstrap\Definition\ModuleDefinition' )
			->disableOriginalConstructor()
			->setMethods( array( 'get' ) )
			->getMock();

		$moduleDefinition->expects( $this->atLeastOnce() )
			->method( 'get' )
			->will( $this->returnValue( array() ) );

		$instance = new BootstrapManager( $moduleDefinition );
		$instance-> addCacheTriggerFile( array( 'foo' => 'bar') );
		$instance-> addCacheTriggerFile( 'ichi' );

		$triggers = $this->getGlobalResourceModuleBootstrapCacheTriggers();

		$this->assertTrue(
			isset( $triggers[ 'foo' ] ) && $triggers[ 'foo' ] === 'bar'
		);

		$this->assertTrue(
			array_search( 'ichi', $triggers ) !== false
		);
	}

	public function testAddExternalModule() {

		$moduleDefinition = $this->getMockBuilder( '\Bootstrap\Definition\ModuleDefinition' )
			->disableOriginalConstructor()
			->setMethods( array( 'get' ) )
			->getMock();

		$moduleDefinition->expects( $this->atLeastOnce() )
			->method( 'get' )
			->will( $this->returnValue( array() ) );

		$instance = new BootstrapManager( $moduleDefinition );
		$instance->addExternalModule( 'ExternalFooModule', 'ExternalRemoteBarPath' );

		$this->assertArrayHasKey(
			'ExternalFooModule',
			$this->getGlobalResourceModuleBootstrapExternalStyles()
		);
	}

	private function getGlobalResourceModuleBootstrapStyles() {
		return $GLOBALS['wgResourceModules'][ 'ext.bootstrap.styles' ]['styles'];
	}

	private function getGlobalResourceModuleBootstrapVariables() {
		return $GLOBALS['wgResourceModules'][ 'ext.bootstrap.styles' ]['variables'];
	}

	private function getGlobalResourceModuleBootstrapExternalStyles() {
		return $GLOBALS['wgResourceModules'][ 'ext.bootstrap.styles' ]['external styles'];
	}

	private function getGlobalResourceModuleBootstrapCacheTriggers() {
		return $GLOBALS['wgResourceModules'][ 'ext.bootstrap.styles' ]['cachetriggers'];
	}

}