Base for a static organization website

ToolbarHelperTest.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * Toolbar facade tests.
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @since DebugKit 0.1
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('View', 'View');
  19. App::uses('Controller', 'Controller');
  20. App::uses('Helper', 'View');
  21. App::uses('ToolbarHelper', 'DebugKit.View/Helper');
  22. App::uses('ConnectionManager', 'Manager');
  23. /**
  24. * Class MockBackendHelper
  25. *
  26. * @since DebugKit 0.1
  27. */
  28. class MockBackendHelper extends Helper {
  29. }
  30. /**
  31. * Class ToolbarHelperTestCase
  32. *
  33. */
  34. class ToolbarHelperTestCase extends CakeTestCase {
  35. /**
  36. * Fixtures
  37. *
  38. * @var array
  39. */
  40. public $fixtures = array('core.post');
  41. /**
  42. * setUp
  43. *
  44. * @return void
  45. */
  46. public function setUp() {
  47. parent::setUp();
  48. $db = ConnectionManager::getDatasource('test');
  49. $db->fullDebug = true;
  50. Configure::write('Cache.disable', false);
  51. Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
  52. Router::parse('/');
  53. $this->Controller = new Controller(null);
  54. $this->View = new View($this->Controller);
  55. $this->Toolbar = new ToolbarHelper($this->View, array(
  56. 'output' => 'MockBackendHelper',
  57. 'cacheKey' => 'debug_kit_toolbar_test_case',
  58. 'cacheConfig' => 'default'
  59. ));
  60. $this->Toolbar->MockBackend = $this->getMock('Helper', array('testMethod'), array($this->View));
  61. $this->_viewPaths = App::path('views');
  62. App::build(array(
  63. 'View' => array(
  64. CAKE_CORE_INCLUDE_PATH . DS . 'Cake' . DS . 'Test' . DS . 'test_app' . DS . 'View' . DS,
  65. APP . 'Plugin' . DS . 'DebugKit' . DS . 'View' . DS,
  66. CAKE_CORE_INCLUDE_PATH . DS . 'Cake' . DS . 'View' . DS
  67. )), true);
  68. }
  69. /**
  70. * tearDown
  71. *
  72. * @return void
  73. */
  74. public function tearDown() {
  75. parent::tearDown();
  76. Cache::delete('debug_kit_toolbar_test_case', 'default');
  77. unset($this->Toolbar, $this->Controller);
  78. }
  79. /**
  80. * test cache writing for views.
  81. *
  82. * @return void
  83. */
  84. public function testCacheWrite() {
  85. $result = $this->Toolbar->writeCache('test', array('stuff', 'to', 'cache'));
  86. $this->assertTrue($result);
  87. }
  88. /**
  89. * Ensure that the cache writing only affects the
  90. * top most level of the history stack. As this is where the current request is stored.
  91. *
  92. * @return void
  93. */
  94. public function testOnlyWritingToFirstElement() {
  95. $values = array(
  96. array('test' => array('content' => array('first', 'values'))),
  97. array('test' => array('content' => array('second', 'values'))),
  98. );
  99. Cache::write('debug_kit_toolbar_test_case', $values, 'default');
  100. $this->Toolbar->writeCache('test', array('new', 'values'));
  101. $result = $this->Toolbar->readCache('test');
  102. $this->assertEquals($result, array('new', 'values'));
  103. $result = $this->Toolbar->readCache('test', 1);
  104. $this->assertEquals($result, array('second', 'values'));
  105. }
  106. /**
  107. * test cache reading for views
  108. *
  109. * @return void
  110. */
  111. public function testCacheRead() {
  112. $result = $this->Toolbar->writeCache('test', array('stuff', 'to', 'cache'));
  113. $this->assertTrue($result, 'Cache write failed %s');
  114. $result = $this->Toolbar->readCache('test');
  115. $this->assertEquals($result, array('stuff', 'to', 'cache'), 'Cache value is wrong %s');
  116. $result = $this->Toolbar->writeCache('test', array('new', 'stuff'));
  117. $this->assertTrue($result, 'Cache write failed %s');
  118. $result = $this->Toolbar->readCache('test');
  119. $this->assertEquals($result, array('new', 'stuff'), 'Cache value is wrong %s');
  120. }
  121. /**
  122. * Test that reading/writing doesn't work with no cache config.
  123. *
  124. * @return void
  125. */
  126. public function testNoCacheConfigPresent() {
  127. $this->Toolbar = new ToolbarHelper($this->View, array('output' => 'MockBackendHelper'));
  128. $result = $this->Toolbar->writeCache('test', array('stuff', 'to', 'cache'));
  129. $this->assertFalse($result, 'Writing to cache succeeded with no cache config %s');
  130. $result = $this->Toolbar->readCache('test');
  131. $this->assertFalse($result, 'Reading cache succeeded with no cache config %s');
  132. }
  133. /**
  134. * ensure that getQueryLogs works and writes to the cache so the history panel will
  135. * work.
  136. *
  137. * @return void
  138. */
  139. public function testGetQueryLogs() {
  140. $model = new CakeTestModel(array('table' => 'posts', 'alias' => 'Post'));
  141. $model->find('all');
  142. $model->find('first');
  143. $result = $this->Toolbar->getQueryLogs($model->useDbConfig, array('cache' => false));
  144. $this->assertTrue(is_array($result));
  145. $this->assertTrue(count($result) >= 2, 'Should be more than 2 queries in the log %s');
  146. $this->assertTrue(isset($result['queries'][0]['actions']));
  147. $model->find('first');
  148. Cache::delete('debug_kit_toolbar_test_case', 'default');
  149. $result = $this->Toolbar->getQueryLogs($model->useDbConfig, array('cache' => true));
  150. $cached = $this->Toolbar->readCache('sql_log');
  151. $this->assertTrue(isset($cached[$model->useDbConfig]));
  152. $this->assertEquals($cached[$model->useDbConfig]['queries'][0], $result['queries'][0]);
  153. }
  154. }