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

Source for file AlphaFilters_Test.php

Documentation is available at AlphaFilters_Test.php

  1. <?php
  2.  
  3. require_once $config->get('sysRoot').'alpha/util/filters/ClientBlacklistFilter.inc';
  4. require_once $config->get('sysRoot').'alpha/util/filters/ClientTempBlacklistFilter.inc';
  5. require_once $config->get('sysRoot').'alpha/controller/front/FrontController.inc';
  6.  
  7. /**
  8.  * Test cases for implementations of the AlphaFilterInterface
  9.  * 
  10.  * @package alpha::tests
  11.  * @since 1.0
  12.  * @author John Collins <dev@alphaframework.org>
  13.  * @version $Id: AlphaFilters_Test.php 1341 2011-03-17 15:02:02Z 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 AlphaFilters_Test extends PHPUnit_Framework_TestCase {
  52.     /**
  53.      * Blacklisted client string
  54.      * 
  55.      * @var BlacklistedClientObject 
  56.      * @since 1.0
  57.      */
  58.     private $blacklistedClient;
  59.     
  60.     /**
  61.      * A "bad" (banned) user agent string for us to test with
  62.      * 
  63.      * @var string 
  64.      * @since 1.0
  65.      */
  66.     private $badAgent 'curl/7.16.2 (i686-redhat-linux-gnu) libcurl/7.16.2 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.8';
  67.     
  68.     /**
  69.      * Used to keep track of the real user-agent of the user running the tests
  70.      * 
  71.      * @var string 
  72.      * @since 1.0
  73.      */
  74.     private $oldAgent;
  75.     
  76.     /**
  77.      * Used to keep track of the real IP of the user running the tests
  78.      * 
  79.      * @var string 
  80.      * @since 1.0
  81.      */
  82.     private $oldIP;
  83.     
  84.     /**
  85.      * A test BadRequestObject
  86.      * 
  87.      * @var BadRequestObject 
  88.      * @since 1.0
  89.      */
  90.     private $badRequest1;
  91.     
  92.     /**
  93.      * A test BadRequestObject
  94.      * 
  95.      * @var BadRequestObject 
  96.      * @since 1.0
  97.      */
  98.     private $badRequest2;
  99.     
  100.     /**
  101.      * A test BadRequestObject
  102.      * 
  103.      * @var BadRequestObject 
  104.      * @since 1.0
  105.      */
  106.     private $badRequest3;
  107.     
  108.     /**
  109.      * A bad IP address
  110.      * 
  111.      * @var string 
  112.      * @since 1.0
  113.      */
  114.     private $badIP '127.0.0.1';
  115.     
  116.     /**
  117.      * Called before the test functions will be executed
  118.      * this function is defined in PHPUnit_TestCase and overwritten
  119.      * here
  120.      * 
  121.      * @since 1.0
  122.      */
  123.     protected function setUp({
  124.         $this->blacklistedClient new BlacklistedClientObject();
  125.         $this->blacklistedClient->rebuildTable();
  126.         $this->blacklistedClient->set('client'$this->badAgent);
  127.         $this->blacklistedClient->save();
  128.         
  129.         $this->badRequest1 new BadRequestObject();
  130.         $this->badRequest1->rebuildTable();
  131.         $this->badRequest1->set('client'$this->badAgent);
  132.         $this->badRequest1->set('IP'$this->badIP);
  133.         $this->badRequest1->set('requestedResource''/doesNotExist');
  134.         $this->badRequest1->save();
  135.         
  136.         $this->badRequest2 new BadRequestObject();
  137.         $this->badRequest2->set('client'$this->badAgent);
  138.         $this->badRequest2->set('IP'$this->badIP);
  139.         $this->badRequest2->set('requestedResource''/doesNotExist');
  140.         $this->badRequest2->save();
  141.         
  142.         $this->badRequest3 new BadRequestObject();
  143.         $this->badRequest3->set('client'$this->badAgent);
  144.         $this->badRequest3->set('IP'$this->badIP);
  145.         $this->badRequest3->set('requestedResource''/doesNotExist');
  146.         $this->badRequest3->save();
  147.         
  148.         $this->oldAgent $_SERVER['HTTP_USER_AGENT'];
  149.         $this->oldIP $_SERVER['REMOTE_ADDR'];
  150.     }
  151.     
  152.     /** 
  153.      * Called after the test functions are executed
  154.      * this function is defined in PHPUnit_TestCase and overwritten
  155.      * here
  156.      * 
  157.      * @since 1.0
  158.      */    
  159.     protected function tearDown({
  160.         $this->blacklistedClient->dropTable();
  161.         unset($this->blacklistedClient);
  162.         
  163.         $this->badRequest1->dropTable();
  164.         unset($this->badRequest1);
  165.         
  166.         unset($this->badRequest2);
  167.         
  168.         unset($this->badRequest3);
  169.         
  170.         $_SERVER['HTTP_USER_AGENT'$this->oldAgent;
  171.         $_SERVER['REMOTE_ADDR'$this->oldIP;
  172.     }
  173.     
  174.     /**
  175.      * Testing that a blacklisted user agent string cannot pass the ClientBlacklistFilter filter
  176.      * 
  177.      * @since 1.0
  178.      */
  179.     public function testClientBlacklistFilter({
  180.         $_SERVER['HTTP_USER_AGENT'$this->badAgent;
  181.         $_GET['act''Search';
  182.         
  183.         try {
  184.             $front new FrontController();
  185.             $front->registerFilter(new ClientBlacklistFilter());
  186.             $front->loadController(false);
  187.             $this->fail('Testing that a blacklisted user agent string cannot pass the ClientBlacklistFilter filter');
  188.         }catch (ResourceNotAllowedException $e{
  189.             $this->assertEquals('Not allowed!'$e->getMessage()'Testing that a blacklisted user agent string cannot pass the ClientBlacklistFilter filter');
  190.         }
  191.     }
  192.     
  193.     /**
  194.      * Testing that a user agent string/IP compbo cannot pass the ClientTempBlacklistFilter filter beyond the config limit
  195.      * 
  196.      * @since 1.0
  197.      */
  198.     public function testClientTempBlacklistFilter({
  199.         global $config;
  200.         $config->set('sysTempBlacklistFilerLimit'3);
  201.         
  202.         $_SERVER['HTTP_USER_AGENT'$this->badAgent;
  203.         $_SERVER['REMOTE_ADDR'$this->badIP;
  204.         $_GET['act''doesNotExist';
  205.         
  206.         try {
  207.             $front new FrontController();
  208.             $front->registerFilter(new ClientTempBlacklistFilter());
  209.             $front->loadController(false);
  210.             $this->fail('Testing that a user agent string/IP compbo cannot pass the ClientTempBlacklistFilter filter beyond the config limit');
  211.         }catch (ResourceNotAllowedException $e{
  212.             $this->assertEquals('Not allowed!'$e->getMessage()'Testing that a user agent string/IP compbo cannot pass the ClientTempBlacklistFilter filter beyond the config limit');
  213.         }
  214.     }
  215. }
  216.  
  217. ?>

Documentation generated on Thu, 17 Mar 2011 16:43:33 +0000 by phpDocumentor 1.4.3