summaryrefslogtreecommitdiffstats
path: root/tests/codeigniter/core/Config_test.php
blob: be426d070a0154c9da70e8cbc252bef9de713293 (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php

class Config_test extends CI_TestCase {

	public function set_up()
	{
		$cls =& $this->ci_core_class('cfg');

		// set predictable config values
		$this->cfg = array(
			'index_page'		=> 'index.php',
			'base_url'		=> 'http://example.com/',
			'subclass_prefix'	=> 'MY_'
		);
		$this->ci_set_config($this->cfg);

		$this->config = new $cls;
	}

	// --------------------------------------------------------------------

	public function test_item()
	{
		$this->assertEquals($this->cfg['base_url'], $this->config->item('base_url'));

		// Bad Config value
		$this->assertFalse($this->config->item('no_good_item'));

		// Index
		$this->assertFalse($this->config->item('no_good_item', 'bad_index'));
		$this->assertFalse($this->config->item('no_good_item', 'default'));
	}

	// --------------------------------------------------------------------

	public function test_set_item()
	{
		$this->assertFalse($this->config->item('not_yet_set'));

		$this->config->set_item('not_yet_set', 'is set');
		$this->assertEquals('is set', $this->config->item('not_yet_set'));
	}

	// --------------------------------------------------------------------

	public function test_slash_item()
	{
		// Bad Config value
		$this->assertFalse($this->config->slash_item('no_good_item'));

		$this->assertEquals($this->cfg['base_url'], $this->config->slash_item('base_url'));
		$this->assertEquals($this->cfg['subclass_prefix'].'/', $this->config->slash_item('subclass_prefix'));
	}

	// --------------------------------------------------------------------

	public function test_base_url()
	{
		// Test regular base URL
		$base_url = $this->cfg['base_url'];
		$this->assertEquals($base_url, $this->config->base_url());

		// Test with URI
		$uri = 'test';
		$this->assertEquals($base_url.$uri, $this->config->base_url($uri));

		// Clear base_url
		$this->ci_set_config('base_url', '');

		// Rerun constructor
		$cls =& $this->ci_core_class('cfg');
		$this->config = new $cls;

		// Test default base
		$this->assertEquals('http://localhost/', $this->config->base_url());

		// Capture server vars
		$old_host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : NULL;
		$old_script = isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : NULL;
		$old_https = isset($_SERVER['HTTPS']) ? $_SERVER['HTTPS'] : NULL;

		// Setup server vars for detection
		$host = 'test.com';
		$path = '/path/';
		$script = 'base_test.php';
		$_SERVER['HTTP_HOST'] = $host;
		$_SERVER['SCRIPT_NAME'] = $path.$script;

		// Rerun constructor
		$this->config = new $cls;

		// Test plain detected
		$this->assertEquals('http://'.$host.$path, $this->config->base_url());

		// Rerun constructor
		$_SERVER['HTTPS'] = 'on';
		$this->config = new $cls;

		// Test secure detected
		$this->assertEquals('https://'.$host.$path, $this->config->base_url());

		// Restore server vars
		if ($old_host === NULL) unset($_SERVER['HTTP_HOST']);
		else $_SERVER['HTTP_HOST'] = $old_host;
		if ($old_script === NULL) unset($_SERVER['SCRIPT_NAME']);
		else $_SERVER['SCRIPT_NAME'] = $old_script;
		if ($old_https === NULL) unset($_SERVER['HTTPS']);
		else $_SERVER['HTTPS'] = $old_https;
	}

	// --------------------------------------------------------------------

	public function test_site_url()
	{
		$base_url = $this->cfg['base_url'];
		$index_page = $this->cfg['index_page'];
		$this->assertEquals($base_url.$index_page, $this->config->site_url());

		$old_base = $this->config->item('base_url');
		$this->config->set_item('base_url', '');

		$q_string = $this->config->item('enable_query_strings');
		$this->config->set_item('enable_query_strings', FALSE);

		$uri = 'test';
		$uri2 = '1';
		$this->assertEquals($index_page.'/'.$uri, $this->config->site_url($uri));
		$this->assertEquals($index_page.'/'.$uri.'/'.$uri2, $this->config->site_url(array($uri, $uri2)));

		$suffix = 'ing';
		$this->config->set_item('url_suffix', $suffix);

		$arg = 'pass';
		$this->assertEquals($index_page.'/'.$uri.$suffix, $this->config->site_url($uri));
		$this->assertEquals($index_page.'/'.$uri.$suffix.'?'.$arg, $this->config->site_url($uri.'?'.$arg));

		$this->config->set_item('url_suffix', FALSE);
		$this->config->set_item('enable_query_strings', TRUE);

		$this->assertEquals($index_page.'?'.$uri, $this->config->site_url($uri));
		$this->assertEquals($index_page.'?0='.$uri.'&1='.$uri2, $this->config->site_url(array($uri, $uri2)));

		$this->config->set_item('base_url', $old_base);

		$this->assertEquals($base_url.$index_page.'?'.$uri, $this->config->site_url($uri));

		// back to home base
		$this->config->set_item('enable_query_strings', $q_string);
	}

	// --------------------------------------------------------------------

	public function test_system_url()
	{
		$this->assertEquals($this->cfg['base_url'].'system/', $this->config->system_url());
	}

	// --------------------------------------------------------------------

	public function test_load()
	{
		// Test regular load
		$file = 'test.php';
		$key = 'testconfig';
		$val = 'my_value';
		$cfg = array($key => $val);
		$this->ci_vfs_create($file, '<?php $config = '.var_export($cfg, TRUE).';', $this->ci_app_root, 'config');
		$this->assertTrue($this->config->load($file));
		$this->assertEquals($val, $this->config->item($key));

		// Test reload - value should not change
		$val2 = 'new_value';
		$cfg = array($key => $val2);
		$this->ci_vfs_create($file, '<?php $config = '.var_export($cfg, TRUE).';', $this->ci_app_root, 'config');
		$this->assertTrue($this->config->load($file));
		$this->assertEquals($val, $this->config->item($key));

		// Test section load
		$file = 'secttest';
		$cfg = array(
			'one' => 'prime',
			'two' => 2,
			'three' => true
		);
		$this->ci_vfs_create($file.'.php', '<?php $config = '.var_export($cfg, TRUE).';', $this->ci_app_root, 'config');
		$this->assertTrue($this->config->load($file, TRUE));
		$this->assertEquals($cfg, $this->config->item($file));

		// Test section merge
		$cfg2 = array(
			'three' => 'tres',
			'number' => 42,
			'letter' => 'Z'
		);
		$pkg_dir = 'package';
		$this->ci_vfs_create($file.'.php', '<?php $config = '.var_export($cfg2, TRUE).';', $this->ci_app_root,
			array($pkg_dir, 'config'));
		array_push($this->config->_config_paths, $this->ci_vfs_path($pkg_dir.'/', APPPATH));
		$this->assertTrue($this->config->load($file, TRUE));
		$this->assertEquals(array_merge($cfg, $cfg2), $this->config->item($file));
		array_pop($this->config->_config_paths);

		// Test graceful fail of invalid file
		$file = 'badfile';
		$this->ci_vfs_create($file, '', $this->ci_app_root, 'config');
		$this->assertFalse($this->config->load($file, FALSE, TRUE));

		// Test regular fail of invalid file
		$this->setExpectedException(
			'RuntimeException',
			'CI Error: Your '.$this->ci_vfs_path('config/'.$file.'.php', APPPATH).
				' file does not appear to contain a valid configuration array.'
		);
		$this->assertNull($this->config->load($file));
	}

	// --------------------------------------------------------------------

	public function test_load_nonexistent()
	{
		// Test graceful fail of nonexistent file
		$this->assertFalse($this->config->load('not_config_file', FALSE, TRUE));

		// Test regular fail
		$file = 'absentia';
		$this->setExpectedException(
			'RuntimeException',
			'CI Error: The configuration file '.$file.'.php does not exist.'
		);
		$this->assertNull($this->config->load($file));
	}

}