1: <?php
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: class FrontController_Test extends PHPUnit_Framework_TestCase {
49: 50: 51: 52: 53: 54:
55: private $token;
56:
57: 58: 59: 60: 61: 62:
63: protected function setUp() {
64: if(!isset($this->token))
65: $this->token = $_GET['tk'];
66: $_GET['tk'] = null;
67: $_GET['act'] = null;
68: }
69:
70: 71: 72: 73: 74: 75:
76: protected function tearDown() {
77: $_GET['tk'] = $this->token;
78: }
79:
80: 81: 82: 83: 84:
85: public function testConstructActParam() {
86: $_GET['act'] = 'ViewArticle';
87: $front = new FrontController();
88:
89: $this->assertEquals('ViewArticle', $front->getPageController(), 'testing that the constructor will detect the page controller action we want to invoke from the global _GET array');
90: }
91:
92: 93: 94: 95: 96:
97: public function testConstructModRewrite() {
98: global $config;
99:
100: $request = $config->get('app.url').'ViewArticleTitle/title/Test_Title';
101: $_SERVER['REQUEST_URI'] = str_replace('http://'.$_SERVER['HTTP_HOST'], '', $request);
102: $front = new FrontController();
103:
104: $this->assertEquals('ViewArticleTitle', $front->getPageController(), 'testing that the constructor can parse the correct page controller action from a mod_rewrite style URL');
105: }
106:
107: 108: 109: 110: 111:
112: public function testConstructModRewriteWithAlias() {
113: global $config;
114:
115: $request = $config->get('app.url').'article/Test_Title';
116: $_SERVER['REQUEST_URI'] = str_replace('http://'.$_SERVER['HTTP_HOST'], '', $request);
117: $front = new FrontController();
118: $front->registerAlias('ViewArticleTitle','article','title');
119:
120: $this->assertEquals('ViewArticleTitle', $front->getPageController(), 'testing that the constructor can parse the correct page controller action from a mod_rewrite style URL when a controller alias is used');
121: }
122:
123: 124: 125: 126: 127:
128: public function testConstructorWithEncryptedToken() {
129: global $config;
130:
131: $params = 'act=ViewArticleTitle&title=Test_Title';
132: $_GET['tk'] = FrontController::encodeQuery($params);
133: $front = new FrontController();
134:
135: $this->assertEquals('ViewArticleTitle', $front->getPageController(), 'testing that the constructor can parse the correct page controller action from an encrypted token param');
136: }
137:
138: 139: 140: 141: 142:
143: public function testConstructorModRewriteWithEncryptedToken() {
144: global $config;
145:
146: $params = 'act=ViewArticleTitle&title=Test_Title';
147: $request = $config->get('app.url').'tk/'.FrontController::encodeQuery($params);
148: $_SERVER['REQUEST_URI'] = str_replace('http://'.$_SERVER['HTTP_HOST'], '', $request);
149: $front = new FrontController();
150:
151: $this->assertEquals('ViewArticleTitle', $front->getPageController(), 'testing that the constructor can parse the correct page controller action from an encrypted token param provided on a mod-rewrite style URL');
152: }
153:
154: 155: 156: 157: 158:
159: public function testEncodeQuery() {
160: global $config;
161:
162: $oldKey = $config->get('security.encryption.key');
163: $config->set('security.encryption.key', 'testkey');
164: $params = 'act=ViewArticleTitle&title=Test_Title';
165:
166: $this->assertEquals(FrontController::encodeQuery($params), '8kqoeebEej0V-FN5-DOdA1HBDDieFcNWTib2yLSUNjq0B0FWzAupIA==', 'testing the encodeQuery method with a known encrypted result for a test key');
167:
168: $config->set('security.encryption.key', $oldKey);
169: }
170:
171: 172: 173: 174: 175:
176: public function testDecodeQueryParams() {
177: global $config;
178:
179: $oldKey = $config->get('security.encryption.key');
180: $config->set('security.encryption.key', 'testkey');
181: $tk = '8kqoeebEej0V-FN5-DOdA1HBDDieFcNWTib2yLSUNjq0B0FWzAupIA==';
182:
183: $this->assertEquals('act=ViewArticleTitle&title=Test_Title', FrontController::decodeQueryParams($tk), 'testing the decodeQueryParams method with a known encrypted result for a test key');
184:
185: $config->set('security.encryption.key', $oldKey);
186: }
187:
188: 189: 190: 191: 192:
193: public function testGetDecodeQueryParams() {
194: global $config;
195:
196: $oldKey = $config->get('security.encryption.key');
197: $config->set('security.encryption.key', 'testkey');
198: $tk = '8kqoeebEej0V-FN5-DOdA1HBDDieFcNWTib2yLSUNjq0B0FWzAupIA==';
199:
200: $decoded = FrontController::getDecodeQueryParams($tk);
201:
202: $this->assertEquals('ViewArticleTitle', $decoded['act'], 'testing that the getDecodeQueryParams method will return the known params with a known encrypted result for a test key');
203: $this->assertEquals('Test_Title', $decoded['title'], 'testing that the getDecodeQueryParams method will return the known params with a known encrypted result for a test key');
204:
205: $config->set('security.encryption.key', $oldKey);
206: }
207:
208: 209: 210: 211: 212:
213: public function testLoadControllerFileNotFound() {
214: global $config;
215:
216: $request = $config->get('app.url').'doesNotExists';
217: $_SERVER['REQUEST_URI'] = str_replace('http://'.$_SERVER['HTTP_HOST'], '', $request);
218: $front = new FrontController();
219:
220: try{
221: $front->loadController(false);
222: $this->fail('testing that a request to a bad URL will result in a ResourceNotFoundException exception');
223: }catch (ResourceNotFoundException $e) {
224: $this->assertTrue($e->getMessage() != '', 'testing that a request to a bad URL will result in a ResourceNotFoundException exception');
225: }
226: }
227:
228: 229: 230: 231: 232:
233: public function testDefineAlias() {
234: $front = new FrontController();
235: $front->registerAlias('ViewArticleTitle','article','title');
236:
237: $this->assertTrue($front->hasAlias('ViewArticleTitle'), 'testing the setting up and checking for the existence of a controller alias');
238: $this->assertTrue($front->checkAlias('article'), 'testing the setting up and checking for the existence of a controller alias');
239: $this->assertEquals('ViewArticleTitle', $front->getAliasController('article'),
240: 'testing the setting up and checking for the existence of a controller alias');
241: $this->assertEquals('article', $front->getControllerAlias('ViewArticleTitle'),
242: 'testing the setting up and checking for the existence of a controller alias');
243: }
244:
245: 246: 247: 248: 249:
250: public function testAccessingAliasParamNames() {
251: $front = new FrontController();
252: $front->registerAlias('ViewArticleTitle','article','title');
253:
254: $this->assertEquals('title', $front->getAliasParam('article'), 'testing the accessing of the expected param for a given alias/controller');
255: $this->assertEquals('title', $front->getControllerParam('ViewArticleTitle'), 'testing the accessing of the expected param for a given alias/controller');
256: }
257:
258: 259: 260: 261: 262:
263: public function testRegisterFilterGood() {
264: try {
265: $front = new FrontController();
266: $front->registerFilter(new ClientBlacklistFilter());
267:
268: $found = false;
269:
270: foreach ($front->getFilters() as $filter) {
271: if($filter instanceof ClientBlacklistFilter)
272: $found = true;
273: }
274: $this->assertTrue($found, 'testing the registerFilter method with a valid filter object');
275: }catch (IllegalArguementException $e) {
276: $this->fail('testing the registerFilter method with a valid filter object');
277: }
278: }
279:
280: 281: 282: 283: 284:
285: public function testRegisterFilterBad() {
286: try {
287: $front = new FrontController();
288: $front->registerFilter(new FrontController());
289:
290: $this->fail('testing the registerFilter method with a bad filter object');
291: }catch (IllegalArguementException $e) {
292: $this->assertEquals('Supplied filter object is not a valid AlphaFilterInterface instance!', $e->getMessage(), 'testing the registerFilter method with a bad filter object');
293: }
294: }
295:
296: 297: 298: 299: 300:
301: public function testGenerateSecureURL() {
302:
303: global $config;
304:
305: $oldKey = $config->get('security.encryption.key');
306: $oldRewriteSetting = $config->get('app.use.mod.rewrite');
307:
308: $config->set('security.encryption.key', 'testkey');
309: $params = 'act=ViewArticleTitle&title=Test_Title';
310:
311: $config->set('app.use.mod.rewrite', true);
312: $this->assertEquals($config->get('app.url').'tk/8kqoeebEej0V-FN5-DOdA1HBDDieFcNWTib2yLSUNjq0B0FWzAupIA==', FrontController::generateSecureURL($params), 'Testing the generateSecureURL() returns the correct URL with mod_rewrite style URLs enabled');
313:
314: $config->set('app.use.mod.rewrite', false);
315: $this->assertEquals($config->get('app.url').'?tk=8kqoeebEej0V-FN5-DOdA1HBDDieFcNWTib2yLSUNjq0B0FWzAupIA==', FrontController::generateSecureURL($params), 'Testing the generateSecureURL() returns the correct URL with mod_rewrite style URLs disabled');
316:
317: $config->set('security.encryption.key', $oldKey);
318: $config->set('app.use.mod.rewrite', $oldRewriteSetting);
319: }
320: }
321:
322: ?>