Overview

Packages

  • alpha::controller
  • alpha::controller::front
  • alpha::exceptions
  • alpha::model
  • alpha::model::types
  • alpha::tasks
  • alpha::tests
  • alpha::util
  • alpha::util::cache
  • alpha::util::codehighlight
  • alpha::util::convertors
  • alpha::util::feeds
  • alpha::util::filters
  • alpha::util::graphs
  • alpha::util::helpers
  • alpha::util::metrics
  • alpha::util::search
  • alpha::view
  • alpha::view::renderers
  • alpha::view::widgets

Classes

  • AlphaAgentUtils_Test
  • AlphaConfig_Test
  • AlphaController_Test
  • AlphaDAO_Test
  • AlphaDAOProviderFactory_Test
  • AlphaFeed_Test
  • AlphaFilters_Test
  • AlphaPHPServerUtils_Test
  • AlphaValidator_Test
  • AlphaView_Test
  • Boolean_Test
  • Date_Test
  • DEnum_Test
  • Double_Test
  • Enum_Test
  • Exceptions_Test
  • FrontController_Test
  • Image_Test
  • Integer_Test
  • Relation_Test
  • RelationLookup_Test
  • SearchProviderTags_Test
  • Sequence_Test
  • String_Test
  • Tag_Test
  • Text_Test
  • Timestamp_Test
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  1: <?php
  2: 
  3: /**
  4:  *
  5:  * Test cases for the AlphaController class.
  6:  *
  7:  * @package alpha::tests
  8:  * @since 1.0
  9:  * @author John Collins <dev@alphaframework.org>
 10:  * @version $Id: FrontController_Test.php 1693 2013-12-09 23:33:24Z alphadevx $
 11:  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
 12:  * @copyright Copyright (c) 2012, John Collins (founder of Alpha Framework).
 13:  * All rights reserved.
 14:  *
 15:  * <pre>
 16:  * Redistribution and use in source and binary forms, with or
 17:  * without modification, are permitted provided that the
 18:  * following conditions are met:
 19:  *
 20:  * * Redistributions of source code must retain the above
 21:  *   copyright notice, this list of conditions and the
 22:  *   following disclaimer.
 23:  * * Redistributions in binary form must reproduce the above
 24:  *   copyright notice, this list of conditions and the
 25:  *   following disclaimer in the documentation and/or other
 26:  *   materials provided with the distribution.
 27:  * * Neither the name of the Alpha Framework nor the names
 28:  *   of its contributors may be used to endorse or promote
 29:  *   products derived from this software without specific
 30:  *   prior written permission.
 31:  *
 32:  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
 33:  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 34:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 35:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 36:  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 37:  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 38:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 39:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 40:  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 41:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 42:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 43:  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 44:  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 45:  * </pre>
 46:  *
 47:  */
 48: class FrontController_Test extends PHPUnit_Framework_TestCase {
 49:     /**
 50:      * A controller token to test with
 51:      *
 52:      * @var string
 53:      * @since 1.0
 54:      */
 55:     private $token;
 56: 
 57:     /**
 58:      * (non-PHPdoc)
 59:      * @see alpha/lib/PEAR/PHPUnit-3.2.9/PHPUnit/Framework/PHPUnit_Framework_TestCase::setUp()
 60:      *
 61:      * @since 1.0
 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:      * (non-PHPdoc)
 72:      * @see alpha/lib/PEAR/PHPUnit-3.2.9/PHPUnit/Framework/PHPUnit_Framework_TestCase::tearDown()
 73:      *
 74:      * @since 1.0
 75:      */
 76:     protected function tearDown() {
 77:         $_GET['tk'] = $this->token;
 78:     }
 79: 
 80:     /**
 81:      * Testing that the constructor will detect the page controller action we want to invoke from the global _GET array
 82:      *
 83:      * @since 1.0
 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:      * Testing that the constructor can parse the correct page controller action from a mod_rewrite style URL
 94:      *
 95:      * @since 1.0
 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:      * Testing that the constructor can parse the correct page controller action from a mod_rewrite style URL when a controller alias is used
109:      *
110:      * @since 1.0
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:      * Testing that the constructor can parse the correct page controller action from an encrypted token param
125:      *
126:      * @since 1.0
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:      * Testing that the constructor can parse the correct page controller action from an encrypted token param provided on a mod-rewrite style URL
140:      *
141:      * @since 1.0
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:      * Testing the encodeQuery method with a known encrypted result for a test key
156:      *
157:      * @since 1.0
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:      * Testing the decodeQueryParams method with a known encrypted result for a test key
173:      *
174:      * @since 1.0
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:      * Testing that the getDecodeQueryParams method will return the known params with a known encrypted result for a test key
190:      *
191:      * @since 1.0
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:      * Testing that a request to a bad URL will result in a ResourceNotFoundException exception
210:      *
211:      * @since 1.0
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:         $badrequest = new BadRequestObject();
221:         if (!$badrequest->checkTableExists())
222:             $badrequest->makeTable();
223: 
224:         try{
225:             $front->loadController(false);
226:             $this->fail('testing that a request to a bad URL will result in a ResourceNotFoundException exception');
227:         }catch (ResourceNotFoundException $e) {
228:             $this->assertTrue($e->getMessage() != '', 'testing that a request to a bad URL will result in a ResourceNotFoundException exception');
229:         }
230:     }
231: 
232:     /**
233:      * Testing the setting up and checking for the existence of a controller alias
234:      *
235:      * @since 1.0
236:      */
237:     public function testDefineAlias() {
238:         $front = new FrontController();
239:         $front->registerAlias('ViewArticleTitle','article','title');
240: 
241:         $this->assertTrue($front->hasAlias('ViewArticleTitle'), 'testing the setting up and checking for the existence of a controller alias');
242:         $this->assertTrue($front->checkAlias('article'), 'testing the setting up and checking for the existence of a controller alias');
243:         $this->assertEquals('ViewArticleTitle', $front->getAliasController('article'),
244:             'testing the setting up and checking for the existence of a controller alias');
245:         $this->assertEquals('article', $front->getControllerAlias('ViewArticleTitle'),
246:             'testing the setting up and checking for the existence of a controller alias');
247:     }
248: 
249:     /**
250:      * Testing the accessing of the expected param for a given alias/controller
251:      *
252:      * @since 1.0
253:      */
254:     public function testAccessingAliasParamNames() {
255:         $front = new FrontController();
256:         $front->registerAlias('ViewArticleTitle','article','title');
257: 
258:         $this->assertEquals('title', $front->getAliasParam('article'), 'testing the accessing of the expected param for a given alias/controller');
259:         $this->assertEquals('title', $front->getControllerParam('ViewArticleTitle'), 'testing the accessing of the expected param for a given alias/controller');
260:     }
261: 
262:     /**
263:      * Testing the registerFilter method with a valid filter object
264:      *
265:      * @since 1.0
266:      */
267:     public function testRegisterFilterGood() {
268:         try {
269:             $front = new FrontController();
270:             $front->registerFilter(new ClientBlacklistFilter());
271: 
272:             $found = false;
273: 
274:             foreach ($front->getFilters() as $filter) {
275:                 if($filter instanceof ClientBlacklistFilter)
276:                     $found = true;
277:             }
278:             $this->assertTrue($found, 'testing the registerFilter method with a valid filter object');
279:         }catch (IllegalArguementException $e) {
280:             $this->fail('testing the registerFilter method with a valid filter object');
281:         }
282:     }
283: 
284:     /**
285:      * Testing the registerFilter method with a bad filter object
286:      *
287:      * @since 1.0
288:      */
289:     public function testRegisterFilterBad() {
290:         try {
291:             $front = new FrontController();
292:             $front->registerFilter(new FrontController());
293: 
294:             $this->fail('testing the registerFilter method with a bad filter object');
295:         }catch (IllegalArguementException $e) {
296:             $this->assertEquals('Supplied filter object is not a valid AlphaFilterInterface instance!', $e->getMessage(), 'testing the registerFilter method with a bad filter object');
297:         }
298:     }
299: 
300:     /**
301:      * Testing the generateSecureURL method
302:      *
303:      * @since 1.2.1
304:      */
305:     public function testGenerateSecureURL() {
306: 
307:         global $config;
308: 
309:         $oldKey = $config->get('security.encryption.key');
310:         $oldRewriteSetting = $config->get('app.use.mod.rewrite');
311: 
312:         $config->set('security.encryption.key', 'testkey');
313:         $params = 'act=ViewArticleTitle&title=Test_Title';
314: 
315:         $config->set('app.use.mod.rewrite', true);
316:         $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');
317: 
318:         $config->set('app.use.mod.rewrite', false);
319:         $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');
320: 
321:         $config->set('security.encryption.key', $oldKey);
322:         $config->set('app.use.mod.rewrite', $oldRewriteSetting);
323:     }
324: }
325: 
326: ?>
Alpha Framework 1.2.4 API Documentation API documentation generated by ApiGen 2.8.0