TestAbstract.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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-2010 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-2010 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. private $_oldControllerDir;
  77. /**
  78. * Prepares the environment before running a test
  79. *
  80. */
  81. protected function setUp()
  82. {
  83. $cwd = dirname(__FILE__);
  84. // read navigation config
  85. $this->_files = $cwd . '/_files';
  86. $config = new Zend_Config_Xml($this->_files . '/navigation.xml');
  87. // setup containers from config
  88. $this->_nav1 = new Zend_Navigation($config->get('nav_test1'));
  89. $this->_nav2 = new Zend_Navigation($config->get('nav_test2'));
  90. // setup view
  91. $view = new Zend_View();
  92. $view->setScriptPath($cwd . '/_files/mvc/views');
  93. // setup front
  94. $front = Zend_Controller_Front::getInstance();
  95. $this->_oldControllerDir = $front->getControllerDirectory('test');
  96. $front->setControllerDirectory($cwd . '/_files/mvc/controllers');
  97. // create helper
  98. $this->_helper = new $this->_helperName();
  99. $this->_helper->setView($view);
  100. // set nav1 in helper as default
  101. $this->_helper->setContainer($this->_nav1);
  102. }
  103. /**
  104. * Cleans up the environment after running a test
  105. *
  106. */
  107. protected function tearDown()
  108. {
  109. $front = Zend_Controller_Front::getInstance();
  110. if ($this->_oldControllerDir) {
  111. $front->setControllerDirectory($this->_oldControllerDir, 'test');
  112. } else {
  113. $front->removeControllerDirectory('test');
  114. }
  115. }
  116. /**
  117. * Returns the contens of the exepcted $file
  118. * @param string $file
  119. * @return string
  120. */
  121. protected function _getExpected($file)
  122. {
  123. return file_get_contents($this->_files . '/expected/' . $file);
  124. }
  125. /**
  126. * Sets up ACL
  127. *
  128. * @return Zend_Acl
  129. */
  130. protected function _getAcl()
  131. {
  132. $acl = new Zend_Acl();
  133. $acl->addRole(new Zend_Acl_Role('guest'));
  134. $acl->addRole(new Zend_Acl_Role('member'), 'guest');
  135. $acl->addRole(new Zend_Acl_Role('admin'), 'member');
  136. $acl->addRole(new Zend_Acl_Role('special'), 'member');
  137. $acl->add(new Zend_Acl_Resource('guest_foo'));
  138. $acl->add(new Zend_Acl_Resource('member_foo'), 'guest_foo');
  139. $acl->add(new Zend_Acl_Resource('admin_foo', 'member_foo'));
  140. $acl->add(new Zend_Acl_Resource('special_foo'), 'member_foo');
  141. $acl->allow('guest', 'guest_foo');
  142. $acl->allow('member', 'member_foo');
  143. $acl->allow('admin', 'admin_foo');
  144. $acl->allow('special', 'special_foo');
  145. $acl->allow('special', 'admin_foo', 'read');
  146. return array('acl' => $acl, 'role' => 'special');
  147. }
  148. /**
  149. * Returns translator
  150. *
  151. * @return Zend_Translate
  152. */
  153. protected function _getTranslator()
  154. {
  155. $data = array(
  156. 'Page 1' => 'Side 1',
  157. 'Page 1.1' => 'Side 1.1',
  158. 'Page 2' => 'Side 2',
  159. 'Page 2.3' => 'Side 2.3',
  160. 'Page 2.3.3.1' => 'Side 2.3.3.1',
  161. 'Home' => 'Hjem',
  162. 'Go home' => 'Gå hjem'
  163. );
  164. return new Zend_Translate('array', $data, 'nb_NO');
  165. }
  166. }