TestAbstract.php 5.2 KB

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