1<?php
2/**
3 * Prepare the test setup.
4 */
5require_once __DIR__ . '/Base.php';
6
7/**
8 * @category   Horde
9 * @package    Auth
10 * @subpackage UnitTests
11 */
12
13class Horde_Auth_Unit_Sql_Locks extends Horde_Auth_Unit_Sql_Base
14{
15    protected static $locksMigrator;
16
17    protected static $locks;
18
19    protected static $skip = '';
20
21    public static function setUpBeforeClass()
22    {
23        parent::setUpBeforeClass();
24
25        if (is_dir(__DIR__ .'/../../../../../../Lock/migration')) {
26            $lockMigrationsPath = __DIR__ .'/../../../../../../Lock/migration';
27        } elseif (is_dir(__DIR__ .'/../../../../../../deps/Lock/migration')) {
28            $lockMigrationsPath = __DIR__ .'/../../../../../../deps/Lock/migration';
29            // how would that work for any possible pear_dir ?
30        } else {
31            self::$skip = 'Could not determine path to Horde_Lock migration';
32            return;
33        }
34        self::$locksMigrator = new Horde_Db_Migration_Migrator(
35            self::$db,
36            null,//$logger,
37            array('migrationsPath' => $lockMigrationsPath,
38                  'schemaTableName' => 'horde_lock_schema_info'));
39        self::$locksMigrator->up();
40
41        self::$locks = new Horde_Lock_Sql(array('db' => self::$db));
42
43        self::$auth = new Horde_Auth_Sql(array('db' => self::$db,
44                                                'encryption' => 'plain',
45                                                'lock_api'   => self::$locks
46                                                ));
47
48    }
49
50    public function setUp()
51    {
52        if (!class_exists('Horde_Db')) {
53            $this->markTestSkipped('The Horde_Db package is not installed!');
54        }
55        if (!class_exists('Horde_Lock')) {
56            $this->markTestSkipped('The Horde_Lock package is not installed!');
57        }
58        if (self::$skip) {
59            $this->markTestSkipped(self::$skip);
60        }
61        if (!self::$db) {
62            $this->markTestSkipped(self::$reason);
63        } else {
64            // portability: use DELETE because SQLite has no truncate
65            $sql = "DELETE FROM horde_locks";
66            self::$db->execute($sql);
67        }
68    }
69
70
71     public function testAuthenticate()
72     {
73         $this->assertTrue(self::$auth->authenticate('tux', array('password' => 'fish')));
74     }
75
76
77    public function testLockUserOnceWorks()
78    {
79        self::$auth->lockUser('konqui');
80    }
81
82    /**
83     * @expectedException Horde_Auth_Exception
84     */
85
86    public function testLockUserTwiceFails()
87    {
88        self::$auth->lockUser('konqui');
89        self::$auth->lockUser('konqui');
90    }
91
92    public function testLockCapability()
93    {
94        $this->assertTrue(self::$auth->hasCapability('lock'));
95    }
96
97    public function testLockedUserReportsAsLocked()
98    {
99        self::$auth->lockUser('konqui');
100        $this->assertTrue(self::$auth->isLocked('konqui'));
101    }
102
103    public function testLockedUserCannotLogin()
104    {
105        self::$auth->lockUser('konqui');
106        $this->assertFalse(self::$auth->authenticate('konqui', array('password' => 'kde')));
107    }
108
109    public function testUnlockUnlockedDoesNotThrowException()
110    {
111        self::$auth->unlockUser('konqui');
112        self::$auth->unlockUser('konqui');
113        self::$auth->unlockUser('konqui');
114    }
115
116}
117