1<?php
2/**
3 * Base class for Turba test cases
4 *
5 * @author  Jason M. Felice <jason.m.felice@gmail.com>
6 * @package Turba
7 * @subpackage UnitTests
8 */
9class Turba_TestBase extends PHPUnit_Framework_TestCase {
10
11    var $_driver;
12    var $_driverConfig = array(
13        'title' => "My Address Book",
14        'type' => 'sql',
15        'params' => null,
16        'map' => array(
17            '__key' => 'object_id',
18            '__owner' => 'owner_id',
19            '__type' => 'object_type',
20            '__members' => 'object_members',
21            '__uid' => 'object_uid',
22            'name' => 'object_name',
23            'email' => 'object_email',
24            'alias' => 'object_alias',
25            'homeAddress' => 'object_homeaddress',
26            'workAddress' => 'object_workaddress',
27            'homePhone' => 'object_homephone',
28            'workPhone' => 'object_workphone',
29            'cellPhone' => 'object_cellphone',
30            'fax' => 'object_fax',
31            'title' => 'object_title',
32            'company' => 'object_company',
33            'notes' => 'object_notes',
34            'pgpPublicKey' => 'object_pgppublickey',
35            'smimePublicKey' => 'object_smimepublickey',
36            'freebusyUrl' => 'object_freebusyurl'
37        ),
38        'search' => array(
39            'name',
40            'email'
41        ),
42        'strict' => array(
43            'object_id',
44            'owner_id',
45            'object_type',
46        ),
47        'export' => true,
48        'browse' => true,
49        'use_shares' => false,
50    );
51
52    var $_fixtures = array(array('object_id' => 'aaa',
53                                 'object_type' => 'Object',
54                                 'owner_id' => '1',
55                                 'object_name' => 'Jason Felice',
56                                 'object_company' => 'Cronosys, LLC'),
57                           array('object_id' => 'bbb',
58                                 'object_type' => 'Object',
59                                 'owner_id' => '1',
60                                 'object_name' => 'Joe Fabetes',
61                                 'object_company' => 'Example, Inc.'),
62                           array('object_id' => 'ccc',
63                                 'object_type' => 'Object',
64                                 'owner_id' => '1',
65                                 'object_name' => 'Alice Z',
66                                 'object_company' => 'Example, Inc.'),
67                           array('object_id' => 'ddd',
68                                 'object_type' => 'Object',
69                                 'owner_id' => '1',
70                                 'object_name' => 'Zoe A',
71                                 'object_company' => 'Example, Inc.'),
72                           array('object_id' => 'eee',
73                                 'object_type' => 'Object',
74                                 'owner_id' => '1',
75                                 'object_name' => 'Alan Garrison',
76                                 'object_company' => 'Cronosys, LLC'),
77                           array('object_id' => 'fff',
78                                 'owner_id' => '1',
79                                 'object_type' => 'Group',
80                                 'object_name' => 'Test Group',
81                                 'object_members' => 'a:5:{i:0;s:3:"aaa";i:1;s:3:"bbb";i:2;s:3:"ccc";i:3;s:3:"ddd";i:4;s:3:"eee";}'),
82                           array('object_id' => 'ggg',
83                                 'owner_id' => '1',
84                                 'object_type' => 'Group',
85                                 'object_name' => 'Alpha First Group',
86                                 'object_members' => 'a:4:{i:0;s:3:"aaa";i:1;s:3:"bbb";i:2;s:3:"ccc";i:3;s:3:"ddd";}'));
87
88    var $_sortedByLastname = array('Zoe A', 'Joe Fabetes', 'Jason Felice',
89                                   'Alan Garrison', 'Alice Z');
90    var $_groups = array("Test Group", "Alpha First Group");
91    var $_sortedByCompanyThenLastname = array('Jason Felice', 'Alan Garrison',
92                                              'Zoe A', 'Joe Fabetes',
93                                              'Alice Z');
94    var $_sortedByCompanyThenLastnameDesc = array('Alan Garrison',
95                                                  'Jason Felice',
96                                                  'Alice Z', 'Joe Fabetes',
97                                                  'Zoe A');
98
99    /**
100     * Retrieves an SQL driver instance.
101     *
102     * @return object Initialized Turba_Driver_sql:: instance connected to the
103     *                test database.
104     */
105    function getDriver()
106    {
107        if (is_null($this->_driver)) {
108            $this->_driver = Turba_Driver::factory('_test_sql',
109                                                   $this->getDriverConfig());
110            $this->assertOk($this->_driver);
111            $this->assertOk($this->_driver->_init());
112        }
113        return $this->_driver;
114    }
115
116    function getDriverConfig()
117    {
118        if (is_null($this->_driverConfig['params'])) {
119            $this->_driverConfig['params'] = array_merge(
120                $this->getTestDatabaseSQLDriverConfig(),
121                array('table' => 'hordetest_turba_objects'));
122        }
123
124        return $this->_driverConfig;
125    }
126
127    /**
128     * Gets the driver's database connection
129     *
130     * FIXME: Should use acceptance environment's method of connecting, and
131     * treat the driver more opaquely.
132     *
133     * @return object PEAR DB reference
134     */
135    function getDb()
136    {
137        $driver = $this->getDriver();
138        return $driver->_db;
139    }
140
141    /**
142     * Asserts that the supplied result is not a PEAR_Error
143     *
144     * Fails with a descriptive message if so
145     * @param mixed $result  The value to check
146     * @return boolean  Whether the assertion was successful
147     */
148    function assertOk($result)
149    {
150        if (is_a($result, 'DB_Error')) {
151            $this->fail($result->getDebugInfo());
152            return false;
153        } elseif (is_a($result, 'PEAR_Error')) {
154            $this->fail($result->getMessage());
155            return false;
156        }
157
158        return true;
159    }
160
161    function setUp()
162    {
163        @define('TURBA_BASE', __DIR__ . '/../..');
164        @define('TURBA_TEMPLATES', $GLOBALS['registry']->get('templates', 'turba'));
165        require_once TURBA_BASE . '/lib/Driver.php';
166        require_once TURBA_BASE . '/lib/Object.php';
167        require_once TURBA_BASE . '/lib/List.php';
168        require_once TURBA_BASE . '/lib/Turba.php';
169    }
170
171    function setUpDatabase()
172    {
173        // Create a new test table, overwriting old ones.
174        require_once 'MDB2/Schema.php';
175        $config = $this->getDriverConfig();
176        $manager = MDB2_Schema::factory($config['params']);
177        $defs = $manager->parseDatabaseDefinition(dirname(dirname(__DIR__)) . '/scripts/sql/test.xml',
178                                                  false,
179                                                  array('name' => $config['params']['database']),
180                                                  false);
181        $result = $manager->createTable('hordetest_turba_objects', $defs['tables']['hordetest_turba_objects'], true);
182        $this->assertOk($result);
183
184        foreach ($this->_fixtures as $fixture) {
185            $this->assertOk($this->_insert($fixture));
186        }
187    }
188
189    function _insert($object)
190    {
191        $db = $this->getDb();
192        $sql = "INSERT INTO hordetest_turba_objects " .
193               Horde_Sql::insertValues($db, $object) .
194               ";";
195        $result = $db->query($sql);
196        $this->assertOk($result);
197        return $result;
198    }
199
200
201    function assertSortsList($callback)
202    {
203        $names = $this->_sortedByLastname;
204        sort($names);
205
206        $tests = array(array('order'   => array(array('field' => 'lastname',
207                                                      'ascending' => true)),
208                             'results' => $this->_sortedByLastname),
209                       array('order'   => array(array('field' => 'lastname',
210                                                      'ascending' => false)),
211                             'results' => array_reverse($this->_sortedByLastname)),
212                       array('order'   => array(array('field' => 'company',
213                                                      'ascending' => true),
214                                                array('field' => 'lastname',
215                                                      'ascending' => true)),
216                             'results' => $this->_sortedByCompanyThenLastname),
217                       array('order'   => array(array('field' => 'company',
218                                                      'ascending' => true),
219                                                array('field' => 'lastname',
220                                                      'ascending' => false)),
221                             'results' => $this->_sortedByCompanyThenLastnameDesc),
222                       array('order'   => array(array('field' => 'name',
223                                                      'ascending' => true)),
224                             'results' => $names),
225                       array('order'   => array(array('field' => 'name',
226                                                      'ascending' => false)),
227                             'results' => array_reverse($names)));
228
229        foreach ($tests as $test) {
230            $list = call_user_func($callback, $test['order']);
231            $this->assertOk($list);
232            if (!$this->assertTrue(is_a($list, 'Turba_List'))) {
233                return;
234            }
235            $this->assertOk($list->reset());
236
237            foreach ($test['results'] as $name) {
238                $this->assertTrue($ob = $list->next());
239                if (!$this->assertTrue(is_a($ob, 'Turba_Object'))) {
240                    continue;
241                }
242                $this->assertEqual($name, $ob->getValue('name'));
243            }
244        }
245    }
246
247    function fakeAuth()
248    {
249        /* Turba_Driver::search() is coupled with authentication global
250         * state. */
251        //$_SESSION['__auth'] = array('authenticated' => true,
252        //                            'userId' => '1');
253        $this->assertEqual('1', $GLOBALS['registry']->getAuth());
254    }
255
256    /**
257     * Constructs and returns a Turba_List:: object populated with items
258     *
259     * @return Turba_List
260     */
261    function getList()
262    {
263        $list = new Turba_List();
264        $driver = $this->getDriver();
265        foreach (array('eee', 'ccc', 'ddd', 'bbb', 'aaa') as $id) {
266            $result = $list->insert($driver->getObject($id));
267            $this->assertOk($result);
268        }
269        return $list;
270    }
271
272}
273