PageRenderTime 66ms CodeModel.GetById 41ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/tests/ViewBrowseTest.php

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