1<?php
2/**
3 * @category   Horde
4 * @package    Auth
5 * @subpackage UnitTests
6 */
7
8class Horde_Auth_Unit_Sql_Base extends Horde_Auth_TestCase
9{
10    protected static $db;
11
12    protected static $auth;
13
14    protected static $migrator;
15
16    protected static $reason;
17
18    public static function setUpBeforeClass()
19    {
20        $dir = __DIR__ . '/../../../../../migration/Horde/Auth';
21        if (!is_dir($dir)) {
22            error_reporting(E_ALL & ~E_DEPRECATED);
23            $dir = PEAR_Config::singleton()
24                ->get('data_dir', null, 'pear.horde.org')
25                . '/Horde_Auth/migration';
26            error_reporting(E_ALL | E_STRICT);
27        }
28        self::$migrator = new Horde_Db_Migration_Migrator(
29            self::$db,
30            null,
31            array('migrationsPath' => $dir,
32                  'schemaTableName' => 'horde_auth_schema_info'));
33        self::$migrator->up();
34
35        self::$auth = new Horde_Auth_Sql(array('db' => self::$db, 'encryption' => 'plain'));
36        // Don't rely on auth->addUser as this is the unit under test
37        $row = "INSERT INTO horde_users VALUES ('mozilla', 'liketokyo', NULL, NULL);";
38        self::$db->execute($row);
39        $row = "INSERT INTO horde_users VALUES ('konqui', 'kde', NULL, NULL);";
40        self::$db->execute($row);
41        $row = "INSERT INTO horde_users VALUES ('tux', 'fish', NULL, NULL);";
42        self::$db->execute($row);
43    }
44
45    public static function tearDownAfterClass()
46    {
47        if (self::$migrator) {
48            self::$migrator->down();
49        }
50        if (self::$db) {
51            self::$db->disconnect();
52            self::$db = null;
53        }
54        parent::tearDownAfterClass();
55    }
56
57    public function setUp()
58    {
59        if (!self::$db) {
60            $this->markTestSkipped(self::$reason);
61        }
62    }
63
64     public function testAuthenticate()
65     {
66         $this->assertTrue(self::$auth->authenticate('tux', array('password' => 'fish')));
67     }
68
69    public function testListUsers()
70    {
71        $resultUnsorted = self::$auth->listUsers();
72        sort($resultUnsorted);
73        $this->assertEquals(array('konqui', 'mozilla', 'tux'), $resultUnsorted);
74    }
75    public function testListUsersWithSorting()
76    {
77        $this->assertEquals(array('konqui', 'mozilla', 'tux'), self::$auth->listUsers(true));
78    }
79
80    public function testLockCapability()
81    {
82        $this->assertFalse(self::$auth->hasCapability('lock'));
83    }
84
85    public function testExistsReturnsTrueForPresentUser()
86    {
87        $this->assertTrue(self::$auth->exists('konqui'));
88    }
89
90    public function testExistsReturnsFalseForMissingUser()
91    {
92        $this->assertFalse(self::$auth->exists('beasty'));
93    }
94}
95