Alpha Framework alpha--tests
[ class tree: alpha--tests ] [ index: alpha--tests ] [ all elements ]

Source for file FrontController_Test.php

Documentation is available at FrontController_Test.php

  1. <?php
  2.  
  3. require_once $config->get('sysRoot').'alpha/controller/front/FrontController.inc';
  4. require_once $config->get('sysRoot').'alpha/util/filters/ClientBlacklistFilter.inc';
  5.  
  6. /**
  7.  *
  8.  * Test cases for the AlphaController class.
  9.  * 
  10.  * @package alpha::tests
  11.  * @since 1.0
  12.  * @author John Collins <dev@alphaframework.org>
  13.  * @version $Id: FrontController_Test.php 1453 2011-12-04 15:12:54Z johnc $
  14.  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
  15.  * @copyright Copyright (c) 2011, John Collins (founder of Alpha Framework).
  16.  *  All rights reserved.
  17.  * 
  18.  *  <pre>
  19.  *  Redistribution and use in source and binary forms, with or
  20.  *  without modification, are permitted provided that the
  21.  *  following conditions are met:
  22.  * 
  23.  *  * Redistributions of source code must retain the above
  24.  *    copyright notice, this list of conditions and the
  25.  *    following disclaimer.
  26.  *  * Redistributions in binary form must reproduce the above
  27.  *    copyright notice, this list of conditions and the
  28.  *    following disclaimer in the documentation and/or other
  29.  *    materials provided with the distribution.
  30.  *  * Neither the name of the Alpha Framework nor the names
  31.  *    of its contributors may be used to endorse or promote
  32.  *    products derived from this software without specific
  33.  *    prior written permission.
  34.  *   
  35.  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  36.  *  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  37.  *  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  38.  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39.  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  40.  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41.  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  42.  *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  43.  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44.  *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  45.  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  46.  *  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  47.  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  48.  *  </pre>
  49.  *  
  50.  */
  51. class FrontController_Test extends PHPUnit_Framework_TestCase {
  52.     /**
  53.      * A controller token to test with
  54.      * 
  55.      * @var string 
  56.      * @since 1.0
  57.      */
  58.     private $token;
  59.         
  60.     /**
  61.      * (non-PHPdoc)
  62.      * @see alpha/lib/PEAR/PHPUnit-3.2.9/PHPUnit/Framework/PHPUnit_Framework_TestCase::setUp()
  63.      * 
  64.      * @since 1.0
  65.      */
  66.     protected function setUp({
  67.         if(!isset($this->token))
  68.             $this->token $_GET['tk'];
  69.         $_GET['tk'null;
  70.         $_GET['act'null;
  71.     }
  72.     
  73.     /**
  74.      * (non-PHPdoc)
  75.      * @see alpha/lib/PEAR/PHPUnit-3.2.9/PHPUnit/Framework/PHPUnit_Framework_TestCase::tearDown()
  76.      * 
  77.      * @since 1.0
  78.      */
  79.     protected function tearDown({
  80.         $_GET['tk'$this->token;
  81.     }
  82.     
  83.     /**
  84.      * Testing that the constructor will detect the page controller action we want to invoke from the global _GET array
  85.      * 
  86.      * @since 1.0
  87.      */
  88.     public function testConstructActParam({
  89.         $_GET['act''ViewArticle';
  90.         $front new FrontController();
  91.         
  92.         $this->assertEquals('ViewArticle'$front->getPageController()'testing that the constructor will detect the page controller action we want to invoke from the global _GET array');
  93.     }
  94.  
  95.     /**
  96.      * Testing that the constructor can parse the correct page controller action from a mod_rewrite style URL
  97.      * 
  98.      * @since 1.0
  99.      */
  100.     public function testConstructModRewrite({
  101.         global $config;
  102.         
  103.         $request $config->get('sysURL').'ViewArticleTitle/title/Test_Title';
  104.         $_SERVER['REQUEST_URI'str_replace('http://'.$_SERVER['HTTP_HOST']''$request);
  105.         $front new FrontController();
  106.         
  107.         $this->assertEquals('ViewArticleTitle'$front->getPageController()'testing that the constructor can parse the correct page controller action from a mod_rewrite style URL');
  108.     }
  109.     
  110.     /**
  111.      * Testing that the constructor can parse the correct page controller action from a mod_rewrite style URL when a controller alias is used
  112.      * 
  113.      * @since 1.0
  114.      */
  115.     public function testConstructModRewriteWithAlias({
  116.         global $config;
  117.         
  118.         $request $config->get('sysURL').'article/Test_Title';
  119.         $_SERVER['REQUEST_URI'str_replace('http://'.$_SERVER['HTTP_HOST']''$request);
  120.         $front new FrontController();
  121.         $front->registerAlias('ViewArticleTitle','article','title');
  122.         
  123.         $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');
  124.     }
  125.     
  126.     /**
  127.      * Testing that the constructor can parse the correct page controller action from an encrypted token param
  128.      * 
  129.      * @since 1.0
  130.      */
  131.     public function testConstructorWithEncryptedToken({
  132.         global $config;
  133.         
  134.         $params 'act=ViewArticleTitle&title=Test_Title';
  135.         $_GET['tk'FrontController::encodeQuery($params);
  136.         $front new FrontController();
  137.         
  138.         $this->assertEquals('ViewArticleTitle'$front->getPageController()'testing that the constructor can parse the correct page controller action from an encrypted token param');
  139.     }
  140.     
  141.     /**
  142.      * Testing that the constructor can parse the correct page controller action from an encrypted token param provided on a mod-rewrite style URL
  143.      * 
  144.      * @since 1.0
  145.      */
  146.     public function testConstructorModRewriteWithEncryptedToken({
  147.         global $config;
  148.         
  149.         $params 'act=ViewArticleTitle&title=Test_Title';
  150.         $request $config->get('sysURL').'tk/'.FrontController::encodeQuery($params);
  151.         $_SERVER['REQUEST_URI'str_replace('http://'.$_SERVER['HTTP_HOST']''$request);
  152.         $front new FrontController();
  153.         
  154.         $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');
  155.     }
  156.     
  157.     /**
  158.      * Testing the encodeQuery method with a known encrypted result for a test key
  159.      * 
  160.      * @since 1.0
  161.      */
  162.     public function testEncodeQuery({
  163.         global $config;
  164.         
  165.         $oldKey $config->get('sysQSKey');
  166.         $config->set('sysQSKey''testkey');
  167.         $params 'act=ViewArticleTitle&title=Test_Title';
  168.         
  169.         $this->assertEquals(FrontController::encodeQuery($params)'8kqoeebEej0V-FN5-DOdA1HBDDieFcNWTib2yLSUNjq0B0FWzAupIA==''testing the encodeQuery method with a known encrypted result for a test key');
  170.         
  171.         $config->set('sysQSKey'$oldKey);
  172.     }
  173.     
  174.     /**
  175.      * Testing the decodeQueryParams method with a known encrypted result for a test key
  176.      * 
  177.      * @since 1.0
  178.      */
  179.     public function testDecodeQueryParams({
  180.         global $config;
  181.         
  182.         $oldKey $config->get('sysQSKey');
  183.         $config->set('sysQSKey''testkey');
  184.         $tk '8kqoeebEej0V-FN5-DOdA1HBDDieFcNWTib2yLSUNjq0B0FWzAupIA==';
  185.         
  186.         $this->assertEquals('act=ViewArticleTitle&title=Test_Title'FrontController::decodeQueryParams($tk)'testing the decodeQueryParams method with a known encrypted result for a test key');
  187.     }
  188.     
  189.     /**
  190.      * Testing that the getDecodeQueryParams method will return the known params with a known encrypted result for a test key
  191.      * 
  192.      * @since 1.0
  193.      */
  194.     public function testGetDecodeQueryParams({
  195.         global $config;
  196.         
  197.         $oldKey $config->get('sysQSKey');
  198.         $config->set('sysQSKey''testkey');
  199.         $tk '8kqoeebEej0V-FN5-DOdA1HBDDieFcNWTib2yLSUNjq0B0FWzAupIA==';
  200.         
  201.         $decoded FrontController::getDecodeQueryParams($tk);
  202.         
  203.         $this->assertEquals('ViewArticleTitle'$decoded['act']'testing that the getDecodeQueryParams method will return the known params with a known encrypted result for a test key');
  204.         $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');
  205.     }
  206.     
  207.     /**
  208.      * Testing that a request to a bad URL will result in a ResourceNotFoundException exception
  209.      * 
  210.      * @since 1.0
  211.      */
  212.     public function testLoadControllerFileNotFound({
  213.         global $config;
  214.         
  215.         $request $config->get('sysURL').'doesNotExists';
  216.         $_SERVER['REQUEST_URI'str_replace('http://'.$_SERVER['HTTP_HOST']''$request);
  217.         $front new FrontController();
  218.         
  219.         try{
  220.             $front->loadController(false);
  221.             $this->fail('testing that a request to a bad URL will result in a ResourceNotFoundException exception');
  222.         }catch (ResourceNotFoundException $e{
  223.             $this->assertTrue($e->getMessage(!= '''testing that a request to a bad URL will result in a ResourceNotFoundException exception');
  224.         }
  225.     }
  226.     
  227.     /**
  228.      * Testing the setting up and checking for the existence of a controller alias
  229.      * 
  230.      * @since 1.0
  231.      */
  232.     public function testDefineAlias({
  233.         $front new FrontController();
  234.         $front->registerAlias('ViewArticleTitle','article','title');
  235.         
  236.         $this->assertTrue($front->hasAlias('ViewArticleTitle')'testing the setting up and checking for the existence of a controller alias');
  237.         $this->assertTrue($front->checkAlias('article')'testing the setting up and checking for the existence of a controller alias');
  238.         $this->assertEquals('ViewArticleTitle'$front->getAliasController('article')
  239.             'testing the setting up and checking for the existence of a controller alias');
  240.         $this->assertEquals('article'$front->getControllerAlias('ViewArticleTitle')
  241.             'testing the setting up and checking for the existence of a controller alias');
  242.     }
  243.     
  244.     /**
  245.      * Testing the accessing of the expected param for a given alias/controller
  246.      * 
  247.      * @since 1.0
  248.      */
  249.     public function testAccessingAliasParamNames({
  250.         $front new FrontController();
  251.         $front->registerAlias('ViewArticleTitle','article','title');
  252.         
  253.         $this->assertEquals('title'$front->getAliasParam('article')'testing the accessing of the expected param for a given alias/controller');
  254.         $this->assertEquals('title'$front->getControllerParam('ViewArticleTitle')'testing the accessing of the expected param for a given alias/controller');
  255.     }
  256.     
  257.     /**
  258.      * Testing the registerFilter method with a valid filter object
  259.      * 
  260.      * @since 1.0
  261.      */
  262.     public function testRegisterFilterGood({
  263.         try {
  264.             $front new FrontController();
  265.             $front->registerFilter(new ClientBlacklistFilter());
  266.             
  267.             $found false;
  268.             
  269.             foreach ($front->getFilters(as $filter{
  270.                 if($filter instanceof ClientBlacklistFilter)
  271.                     $found true;
  272.             }
  273.             $this->assertTrue($found'testing the registerFilter method with a valid filter object');
  274.         }catch (IllegalArguementException $e{
  275.             $this->fail('testing the registerFilter method with a valid filter object');
  276.         }
  277.     }
  278.     
  279.     /**
  280.      * Testing the registerFilter method with a bad filter object
  281.      * 
  282.      * @since 1.0
  283.      */
  284.     public function testRegisterFilterBad({
  285.         try {
  286.             $front new FrontController();
  287.             $front->registerFilter(new FrontController());
  288.             
  289.             $this->fail('testing the registerFilter method with a bad filter object');
  290.         }catch (IllegalArguementException $e{
  291.             $this->assertEquals('Supplied filter object is not a valid AlphaFilterInterface instance!'$e->getMessage()'testing the registerFilter method with a bad filter object');
  292.         }
  293.     }
  294. }
  295.  
  296. ?>

Documentation generated on Tue, 13 Dec 2011 20:26:57 +0000 by phpDocumentor 1.4.3