TestAbstract.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_View
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. require_once 'Zend/Acl.php';
  23. require_once 'Zend/Acl/Resource.php';
  24. require_once 'Zend/Acl/Role.php';
  25. require_once 'Zend/Controller/Front.php';
  26. require_once 'Zend/Config/Xml.php';
  27. require_once 'Zend/Registry.php';
  28. require_once 'Zend/Translate.php';
  29. require_once 'Zend/View.php';
  30. require_once 'Zend/Navigation.php';
  31. /**
  32. * Base class for navigation view helper tests
  33. *
  34. * @category Zend
  35. * @package Zend_View
  36. * @subpackage UnitTests
  37. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. * @group Zend_View
  40. * @group Zend_View_Helper
  41. */
  42. abstract class Zend_View_Helper_Navigation_TestAbstract
  43. extends PHPUnit_Framework_TestCase
  44. {
  45. const REGISTRY_KEY = 'Zend_Navigation';
  46. /**
  47. * Path to files needed for test
  48. *
  49. * @var string
  50. */
  51. protected $_files;
  52. /**
  53. * Class name for view helper to test
  54. *
  55. * @var string
  56. */
  57. protected $_helperName;
  58. /**
  59. * View helper
  60. *
  61. * @var Zend_View_Helper_Navigation_HelperAbstract
  62. */
  63. protected $_helper;
  64. /**
  65. * The first container in the config file (_files/navigation.xml)
  66. *
  67. * @var Zend_Navigation
  68. */
  69. protected $_nav1;
  70. /**
  71. * The second container in the config file (_files/navigation.xml)
  72. *
  73. * @var Zend_Navigation
  74. */
  75. protected $_nav2;
  76. /**
  77. * The third container in the config file (_files/navigation.xml)
  78. *
  79. * @var Zend_Navigation
  80. */
  81. protected $_nav3;
  82. private $_oldControllerDir;
  83. /**
  84. * Prepares the environment before running a test
  85. *
  86. */
  87. protected function setUp()
  88. {
  89. $cwd = dirname(__FILE__);
  90. // read navigation config
  91. $this->_files = $cwd . '/_files';
  92. $config = new Zend_Config_Xml($this->_files . '/navigation.xml');
  93. // setup containers from config
  94. $this->_nav1 = new Zend_Navigation($config->get('nav_test1'));
  95. $this->_nav2 = new Zend_Navigation($config->get('nav_test2'));
  96. $this->_nav3 = new Zend_Navigation($config->get('nav_test3'));
  97. // setup view
  98. $view = new Zend_View();
  99. $view->setScriptPath($cwd . '/_files/mvc/views');
  100. // setup front
  101. $front = Zend_Controller_Front::getInstance();
  102. $this->_oldControllerDir = $front->getControllerDirectory('test');
  103. $front->setControllerDirectory($cwd . '/_files/mvc/controllers');
  104. // create helper
  105. $this->_helper = new $this->_helperName();
  106. $this->_helper->setView($view);
  107. // set nav1 in helper as default
  108. $this->_helper->setContainer($this->_nav1);
  109. }
  110. /**
  111. * Cleans up the environment after running a test
  112. *
  113. */
  114. protected function tearDown()
  115. {
  116. $front = Zend_Controller_Front::getInstance();
  117. if ($this->_oldControllerDir) {
  118. $front->setControllerDirectory($this->_oldControllerDir, 'test');
  119. } else {
  120. $front->removeControllerDirectory('test');
  121. }
  122. }
  123. /**
  124. * Returns the contens of the exepcted $file
  125. * @param string $file
  126. * @return string
  127. */
  128. protected function _getExpected($file)
  129. {
  130. return file_get_contents($this->_files . '/expected/' . $file);
  131. }
  132. /**
  133. * Sets up ACL
  134. *
  135. * @return Zend_Acl
  136. */
  137. protected function _getAcl()
  138. {
  139. $acl = new Zend_Acl();
  140. $acl->addRole(new Zend_Acl_Role('guest'));
  141. $acl->addRole(new Zend_Acl_Role('member'), 'guest');
  142. $acl->addRole(new Zend_Acl_Role('admin'), 'member');
  143. $acl->addRole(new Zend_Acl_Role('special'), 'member');
  144. $acl->add(new Zend_Acl_Resource('guest_foo'));
  145. $acl->add(new Zend_Acl_Resource('member_foo'), 'guest_foo');
  146. $acl->add(new Zend_Acl_Resource('admin_foo', 'member_foo'));
  147. $acl->add(new Zend_Acl_Resource('special_foo'), 'member_foo');
  148. $acl->allow('guest', 'guest_foo');
  149. $acl->allow('member', 'member_foo');
  150. $acl->allow('admin', 'admin_foo');
  151. $acl->allow('special', 'special_foo');
  152. $acl->allow('special', 'admin_foo', 'read');
  153. return array('acl' => $acl, 'role' => 'special');
  154. }
  155. /**
  156. * Returns translator
  157. *
  158. * @return Zend_Translate
  159. */
  160. protected function _getTranslator()
  161. {
  162. $data = array(
  163. 'Page 1' => 'Side 1',
  164. 'Page 1.1' => 'Side 1.1',
  165. 'Page 2' => 'Side 2',
  166. 'Page 2.3' => 'Side 2.3',
  167. 'Page 2.3.3.1' => 'Side 2.3.3.1',
  168. 'Home' => 'Hjem',
  169. 'Go home' => 'Gå hjem'
  170. );
  171. return new Zend_Translate('array', $data, 'nb_NO');
  172. }
  173. }