1<?php
2
3require_once __DIR__ . '/TestBase.php';
4
5/**
6 * @author  Jason M. Felice <jason.m.felice@gmail.com>
7 * @package Turba
8 * @subpackage UnitTests
9 */
10class Turba_ToDo_ViewBrowseTest extends Turba_TestBase {
11
12    function setUp()
13    {
14        $this->markTestIncomplete('Convert to use Horde_Test.');
15        parent::setUp();
16        require_once TURBA_BASE . '/lib/View/Browse.php';
17        $this->setUpDatabase();
18        $this->setUpBrowseView();
19    }
20
21    function setUpBrowseView()
22    {
23        $vars = new Horde_Variables();
24        $notification = $GLOBALS['notification'];
25        $turbaConf = array();
26        $turbaConf['menu']['import_export'] = true;
27        $turbaConf['menu']['apps'] = array();
28        $turbaConf['client']['addressbook'] = '_test_sql';
29        $turbaConf['shares']['source'] = 'foo';
30        $turbaConf['comments']['allow'] = true;
31        $turbaConf['documents']['type'] = 'horde';
32        include TURBA_BASE . '/config/attributes.php';
33
34        $cfgSources = array('_test_sql' => $this->getDriverConfig());
35        $this->_pageParams = array('vars' => $vars,
36                                   'prefs' => $GLOBALS['prefs'],
37                                   'notification' => $notification,
38                                   'registry' => $GLOBALS['registry'],
39                                   'browse_source_count' => 1,
40                                   'browse_source_options' => "My Address Book",
41                                   'copymoveSources' => array(),
42                                   'addSources' => $cfgSources,
43                                   'cfgSources' => $cfgSources,
44                                   'attributes' => $attributes,
45                                   'turba_shares' => false,
46                                   'conf' => $turbaConf,
47                                   'source' => '_test_sql',
48                                   'browser' => $GLOBALS['browser']);
49
50        // These are referenced explicitly from $GLOBALS, *sigh*
51        $GLOBALS['browse_source_count'] = $this->_pageParams['browse_source_count'];
52        $GLOBALS['addSources'] = $cfgSources;
53        $GLOBALS['copymoveSources'] = array();
54        $GLOBALS['cfgSources'] = $cfgSources;
55
56        $this->setPref('addressbooks', json_encode(array('_test_sql')));
57    }
58
59    function getPage()
60    {
61        $this->_pageParams['registry']->pushApp('turba', array('check_perms' => false));
62        $this->fakeAuth();
63        $page = new Turba_View_Browse($this->_pageParams);
64
65        Horde::startBuffer();
66        $page->run();
67        $this->_output = Horde::endBuffer();
68
69        if ($push_result) {
70            $this->_pageParams['registry']->popApp();
71        }
72
73        $this->assertNoUnwantedPattern('/<b>Warning/', $this->_output);
74        $this->assertNoUnwantedPattern('/<b>Fatal error/i', $this->_output);
75
76        return $this->_output;
77    }
78
79    function setPref($name, $value)
80    {
81        $prefs = $this->_pageParams['prefs'];
82        $this->assertOk($prefs->setValue($name, $value));
83        $this->assertEqual($value, $prefs->getValue($name));
84    }
85
86    function getPref($name)
87    {
88        return $this->_pageParams['prefs']->getValue($name);
89    }
90
91    function setVar($name, $value)
92    {
93        $vars = $this->_pageParams['vars'];
94        $vars->set($name, $value);
95    }
96
97    function assertOutputContainsItems($items, $m = 'assertWantedPattern')
98    {
99        $fail = false;
100        foreach ($items as $item) {
101            $pattern = '!>' . preg_quote($item, '!') . '</a>!';
102            if (!$this->$m($pattern, $this->_output)) {
103                $fail = true;
104            }
105        }
106        if ($fail) {
107            print $this->_output;
108        }
109        return !$fail;
110    }
111
112    function assertOutputDoesNotContainItems($items)
113    {
114        return $this->assertOutputContainsItems($items,
115                                                'assertNoUnwantedPattern');
116    }
117
118    function test_getting_page_shows_all_contacts_and_groups_from_test_addressbook()
119    {
120        $this->getPage();
121        $this->assertOutputContainsItems(array_merge($this->_sortedByLastname, $this->_groups));
122    }
123
124    function test_getting_page_with_sort_parameters_updates_sort_preferences()
125    {
126        $this->setPref('sortorder', '');
127        $this->setVar('sortby', '0');
128        $this->setVar('sortdir', '1');
129        $this->getPage();
130        $this->assertEqual(serialize(array(array('field' => 'lastname', 'ascending' => false))),
131                           $this->getPref('sortorder'));
132    }
133
134    function test_getting_page_with_show_equals_contacts_will_show_only_contacts()
135    {
136        $this->setVar('show', 'contacts');
137        $this->getPage();
138        $this->assertOutputContainsItems($this->_sortedByLastname);
139        $this->assertOutputDoesNotContainItems($this->_groups);
140    }
141
142    function test_getting_page_with_show_equals_lists_will_show_only_groups()
143    {
144        $this->setVar('show', 'lists');
145        $this->getPage();
146        $this->assertOutputDoesNotContainItems($this->_sortedByLastname);
147        $this->assertOutputContainsItems($this->_groups);
148    }
149
150    function test_browsing_list_shows_list_members_only()
151    {
152        $groupId = 'ggg';
153        $this->setVar('key', $groupId);
154        $this->getPage();
155
156        $found = false;
157        foreach ($this->_fixtures as $fixture) {
158            if ($fixture['object_id'] == $groupId) {
159                $found = true;
160                $this->assertEqual('Group', $fixture['object_type']);
161                $memberIds = unserialize($fixture['object_members']);
162            }
163        }
164        $this->assertTrue($found);
165
166        $inList = array();
167        $notInList = array();
168        foreach ($this->_fixtures as $fixture) {
169            if ($fixture['object_type'] == 'Object') {
170                if (in_array($fixture['object_id'], $memberIds)) {
171                    $inList[] = $fixture['object_name'];
172                } else {
173                    $notInList[] = $fixture['object_name'];
174                }
175            }
176        }
177
178        $this->assertFalse(empty($inList));
179        $this->assertOutputContainsItems($inList);
180        $this->assertFalse(empty($notInList));
181        $this->assertOutputDoesNotContainItems($notInList);
182    }
183
184}
185