ModuleTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace yiiunit\debug;
  3. use Yii;
  4. use yii\base\Event;
  5. use yii\caching\FileCache;
  6. use yii\debug\Module;
  7. class ModuleTest extends TestCase
  8. {
  9. protected function setUp()
  10. {
  11. parent::setUp();
  12. $this->mockWebApplication();
  13. }
  14. // Tests :
  15. /**
  16. * Data provider for [[testCheckAccess()]]
  17. * @return array test data
  18. */
  19. public function dataProviderCheckAccess()
  20. {
  21. return [
  22. [
  23. [],
  24. '10.20.30.40',
  25. false
  26. ],
  27. [
  28. ['10.20.30.40'],
  29. '10.20.30.40',
  30. true
  31. ],
  32. [
  33. ['*'],
  34. '10.20.30.40',
  35. true
  36. ],
  37. [
  38. ['10.20.30.*'],
  39. '10.20.30.40',
  40. true
  41. ],
  42. [
  43. ['10.20.30.*'],
  44. '10.20.40.40',
  45. false
  46. ],
  47. ];
  48. }
  49. // Tests :
  50. /**
  51. * @dataProvider dataProviderCheckAccess
  52. *
  53. * @param array $allowedIPs
  54. * @param string $userIp
  55. * @param bool $expectedResult
  56. * @throws \ReflectionException
  57. */
  58. public function testCheckAccess(array $allowedIPs, $userIp, $expectedResult)
  59. {
  60. $module = new Module('debug');
  61. $module->allowedIPs = $allowedIPs;
  62. $_SERVER['REMOTE_ADDR'] = $userIp;
  63. $this->assertEquals($expectedResult, $this->invoke($module, 'checkAccess'));
  64. }
  65. /**
  66. * Test to verify toolbars html
  67. */
  68. public function testGetToolbarHtml()
  69. {
  70. $module = new Module('debug');
  71. $module->bootstrap(Yii::$app);
  72. Yii::getLogger()->dispatcher = $this->getMockBuilder('yii\\log\\Dispatcher')
  73. ->setMethods(['dispatch'])
  74. ->getMock();
  75. $this->assertEquals(<<<HTML
  76. <div id="yii-debug-toolbar" data-url="/index.php?r=debug%2Fdefault%2Ftoolbar&amp;tag={$module->logTarget->tag}" style="display:none" class="yii-debug-toolbar-bottom"></div>
  77. HTML
  78. , $module->getToolbarHtml());
  79. }
  80. /**
  81. * Test to ensure toolbar is never cached
  82. */
  83. public function testNonCachedToolbarHtml()
  84. {
  85. $module = new Module('debug');
  86. $module->allowedIPs = ['*'];
  87. Yii::$app->setModule('debug', $module);
  88. $module->bootstrap(Yii::$app);
  89. Yii::getLogger()->dispatcher = $this->getMockBuilder('yii\\log\\Dispatcher')
  90. ->setMethods(['dispatch'])
  91. ->getMock();
  92. Yii::$app->set('cache', new FileCache(['cachePath' => '@yiiunit/debug/runtime/cache']));
  93. $view = Yii::$app->view;
  94. for ($i = 0; $i <= 1; $i++) {
  95. ob_start();
  96. $module->logTarget->tag = 'tag' . $i;
  97. if ($view->beginCache(__FUNCTION__, ['duration' => 3])) {
  98. $module->renderToolbar(new Event(['sender' => $view]));
  99. $view->endCache();
  100. }
  101. $output[$i] = ob_get_clean();
  102. }
  103. $this->assertNotEquals($output[0], $output[1]);
  104. }
  105. /**
  106. * Making sure debug toolbar does not error
  107. * in case module ID is not "debug".
  108. *
  109. * @see https://github.com/yiisoft/yii2-debug/pull/176/
  110. */
  111. public function testToolbarWithCustomModuleID()
  112. {
  113. $moduleID = 'my_debug';
  114. $module = new Module($moduleID);
  115. $module->allowedIPs = ['*'];
  116. Yii::$app->setModule($moduleID, $module);
  117. $module->bootstrap(Yii::$app);
  118. Yii::getLogger()->dispatcher = $this->getMockBuilder('yii\\log\\Dispatcher')
  119. ->setMethods(['dispatch'])
  120. ->getMock();
  121. $view = Yii::$app->view;
  122. ob_start();
  123. $module->renderToolbar(new Event(['sender' => $view]));
  124. $output = ob_get_clean();
  125. $this->assertThat($output, $this->logicalOr(
  126. $this->matches('%Adata-url="/my_debug%A'),
  127. $this->matches('%Adata-url="/index.php?r=my_debug%A')
  128. ));
  129. }
  130. public function testDefaultVersion()
  131. {
  132. Yii::$app->extensions['yiisoft/yii2-debug'] = [
  133. 'name' => 'yiisoft/yii2-debug',
  134. 'version' => '2.0.7',
  135. ];
  136. $module = new Module('debug');
  137. $this->assertEquals('2.0.7', $module->getVersion());
  138. }
  139. }