home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / TestAuthContainer.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  5.8 KB  |  171 lines

  1. <?php
  2.  
  3. include_once 'PHPUnit.php';
  4.  
  5.  
  6. class TestAuthContainer extends PHPUnit_TestCase
  7. {
  8.  
  9.     var $skip_tests = false;
  10.     var $skip_tests_message = "SKIP TEST";
  11.  
  12.     function TestAuthContainer($name)
  13.     {
  14.         $this->PHPUnit_TestCase($name);
  15.         $this->container =& $this->getContainer();
  16.         $this->user = 'joe';
  17.         $this->pass = 'doe';
  18.         $this->opt = 'VeryCoolUser';
  19.     }
  20.  
  21.     // Abstract
  22.     function getContainer() {}
  23.     function getExtraOptions() {}
  24.  
  25.     function setUp()
  26.     {
  27.         $opt = $this->getExtraOptions();
  28.         // Add the default user to be used for some testing
  29.         $this->container->addUser($opt['username'], $opt['passwd']);
  30.     }
  31.  
  32.     function tearDown()
  33.     {
  34.         $opt = $this->getExtraOptions();
  35.         // Remove default user
  36.         $this->container->removeUser($opt['username']);
  37.     }
  38.  
  39.     function testListUsers()
  40.     {
  41.         if ($this->skip_tests) {
  42.             $this->fail($this->skip_tests_message.'');
  43.             return(false);
  44.         }
  45.  
  46.         $users = $this->container->listUsers();
  47.         if (AUTH_METHOD_NOT_SUPPORTED === $users) {
  48.             $this->fail('This operation is not supported by '.get_class($this->container));
  49.             return(false);
  50.         }
  51.  
  52.         $opt = $this->getExtraOptions();
  53.         $this->assertTrue(is_array($users[0]), 'First array element from result was not an array');
  54.         $this->assertTrue($users[0]['username'] == $opt['username'], sprintf('First username was not equal to default username "%s" ', $opt['username']));
  55.     }
  56.  
  57.     function testAddUser()
  58.     {
  59.         if ($this->skip_tests) {
  60.             $this->fail($this->skip_tests_message.'');
  61.             return(false);
  62.         }
  63.  
  64.         $cb = count($this->container->listUsers());
  65.         $res = $this->container->addUser($this->user, $this->pass, $this->opt);
  66.         if (AUTH_METHOD_NOT_SUPPORTED === $res) {
  67.             $this->fail("This operation is not supported by ".get_class($this->container));
  68.             return(false);
  69.         }
  70.  
  71.         if (PEAR::isError($res)) {
  72.             $error = $res->getMessage().' ['.$res->getUserInfo().']';
  73.         } else {
  74.             $error = '';
  75.         }
  76.         $this->assertTrue(!PEAR::isError($res), 'error:'.$error);
  77.         $ca = count($this->container->listUsers());
  78.         $users = $this->container->listUsers();
  79.         $last_username = $users[$ca-1]['username'];
  80.         $this->assertTrue( ($cb === $ca-1) , sprintf('Count of users before (%s) and after (%s) does not differ by one', $cb, $ca));
  81.         $this->assertTrue( $this->container->fetchData($this->user, $this->pass) , sprintf('Could not verify with the newly created user %s',$this->user));
  82.  
  83.         // Remove the user we just added, assumes removeUser works
  84.         $this->container->removeUser($this->user);
  85.     }
  86.  
  87.     function testFetchData()
  88.     {
  89.         if ($this->skip_tests) {
  90.             $this->fail($this->skip_tests_message.'');
  91.             return(false);
  92.         }
  93.  
  94.         $opt = $this->getExtraOptions();
  95.         $fetch_res = $this->container->fetchData($opt['username'], $opt['passwd']);
  96.         if (AUTH_METHOD_NOT_SUPPORTED === $fetch_res) {
  97.             $this->fail("This operation is not supported by ".get_class($this->container));
  98.             return(false);
  99.         }
  100.  
  101.         $this->assertTrue($fetch_res,sprintf('Could not verify with the default username (%s) and passwd (%s)', $opt['username'], $opt['passwd']));
  102.         
  103.         // Test for fail fetchData
  104.         $opt = $this->getExtraOptions();
  105.         $this->assertFalse(
  106.             $this->container->fetchData(md5($opt['username']), $opt['passwd']),
  107.             "fetchData returned true with invalid username and pass"
  108.         );
  109.         
  110.     }
  111.     
  112.     
  113.     /**
  114.      * Tjis test depends on add user & remove user to work
  115.      */
  116.     function testFetchDataSpaceInPassword()
  117.     {
  118.     
  119.         if ($this->skip_tests) {
  120.             $this->fail($this->skip_tests_message.'');
  121.             return(false);
  122.         }
  123.         
  124.         $user = uniqid('user');
  125.         $pass = 'Some Pass ';
  126.         
  127.         $res = $this->container->addUser($user, $pass, array());
  128.         if (AUTH_METHOD_NOT_SUPPORTED === $res) {
  129.             $this->fail("This operation is not supported by ".get_class($this->container));
  130.             return(false);
  131.         } else {
  132.             $fetch_res = $this->container->fetchData($user, $pass);
  133.             if (AUTH_METHOD_NOT_SUPPORTED === $fetch_res) {
  134.                 $this->fail("This operation is not supported by ".get_class($this->container));
  135.                 return(false);
  136.             } else {
  137.                 $this->assertTrue($fetch_res, 'Could not verify user with space password');
  138.             }           
  139.         }
  140.         
  141.         $remove_res = $this->container->removeUser($user);
  142.     }
  143.  
  144.  
  145.  
  146.  
  147.     function testRemoveUser()
  148.     {
  149.         if ($this->skip_tests) {
  150.             $this->fail($this->skip_tests_message.'');
  151.             return(false);
  152.         }
  153.  
  154.         // Add a user to be removed when testing removeUuser method
  155.         // Assume add user works
  156.         $this->container->addUser('for_remove', 'for_remove');
  157.         $cb = count($this->container->listUsers());
  158.         $remove_res = $this->container->removeUser('for_remove');
  159.         if (AUTH_METHOD_NOT_SUPPORTED === $remove_res) {
  160.             $this->fail("This operation is not supported by ".get_class($this->container));
  161.             return(false);
  162.         }
  163.  
  164.         $this->assertTrue(AUTH_METHOD_NOT_SUPPORTED == $remove_res, "This operation is not supported by ".get_class($this));
  165.         $ca = count($this->container->listUsers());
  166.         $this->assertTrue($cb === $ca+1, sprintf('Could not remove user "%s", count before:%s count after:%s ', 'for_remove', $cb, $ca));
  167.     }
  168.  
  169. }
  170.  
  171. ?>