ModuleTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. ['172.16.0.0/12'],
  49. '172.15.1.2', // "below" CIDR range
  50. false
  51. ],
  52. [
  53. ['172.16.0.0/12'],
  54. '172.16.0.0', // in CIDR range
  55. true
  56. ],
  57. [
  58. ['172.16.0.0/12'],
  59. '172.22.33.44', // in CIDR range
  60. true
  61. ],
  62. [
  63. ['172.16.0.0/12'],
  64. '172.31.255.255', // in CIDR range
  65. true
  66. ],
  67. [
  68. ['172.16.0.0/12'],
  69. '172.32.1.2', // "above" CIDR range
  70. false
  71. ],
  72. ];
  73. }
  74. // Tests :
  75. /**
  76. * @dataProvider dataProviderCheckAccess
  77. *
  78. * @param array $allowedIPs
  79. * @param string $userIp
  80. * @param bool $expectedResult
  81. * @throws \ReflectionException
  82. */
  83. public function testCheckAccess(array $allowedIPs, $userIp, $expectedResult)
  84. {
  85. $module = new Module('debug');
  86. $module->allowedIPs = $allowedIPs;
  87. $_SERVER['REMOTE_ADDR'] = $userIp;
  88. $this->assertEquals($expectedResult, $this->invoke($module, 'checkAccess'));
  89. }
  90. /**
  91. * Test to ensure logTarget can take object as config
  92. */
  93. public function testLogTargetObject()
  94. {
  95. $module = new Module('debug');
  96. $module->logTarget = new \yii\debug\LogTarget($module);
  97. $module->bootstrap(Yii::$app);
  98. $this->assertInstanceOf('yii\debug\LogTarget', $module->logTarget);
  99. }
  100. /**
  101. * Test to verify toolbars html
  102. */
  103. public function testGetToolbarHtml()
  104. {
  105. $module = new Module('debug');
  106. $module->bootstrap(Yii::$app);
  107. Yii::getLogger()->dispatcher = $this->getMockBuilder('yii\\log\\Dispatcher')
  108. ->setMethods(['dispatch'])
  109. ->getMock();
  110. $this->assertEquals(<<<HTML
  111. <div id="yii-debug-toolbar" data-url="/index.php?r=debug%2Fdefault%2Ftoolbar&amp;tag={$module->logTarget->tag}" data-skip-urls="[]" style="display:none" class="yii-debug-toolbar-bottom"></div>
  112. HTML
  113. , $module->getToolbarHtml());
  114. }
  115. /**
  116. * Test to ensure toolbar is never cached
  117. */
  118. public function testNonCachedToolbarHtml()
  119. {
  120. $module = new Module('debug');
  121. $module->allowedIPs = ['*'];
  122. Yii::$app->setModule('debug', $module);
  123. $module->bootstrap(Yii::$app);
  124. Yii::getLogger()->dispatcher = $this->getMockBuilder('yii\\log\\Dispatcher')
  125. ->setMethods(['dispatch'])
  126. ->getMock();
  127. Yii::$app->set('cache', new FileCache(['cachePath' => '@yiiunit/debug/runtime/cache']));
  128. $view = Yii::$app->view;
  129. for ($i = 0; $i <= 1; $i++) {
  130. ob_start();
  131. $module->logTarget->tag = 'tag' . $i;
  132. if ($view->beginCache(__FUNCTION__, ['duration' => 3])) {
  133. $module->renderToolbar(new Event(['sender' => $view]));
  134. $view->endCache();
  135. }
  136. $output[$i] = ob_get_clean();
  137. }
  138. $this->assertNotEquals($output[0], $output[1]);
  139. }
  140. /**
  141. * Making sure debug toolbar does not error
  142. * in case module ID is not "debug".
  143. *
  144. * @see https://github.com/yiisoft/yii2-debug/pull/176/
  145. */
  146. public function testToolbarWithCustomModuleID()
  147. {
  148. $moduleID = 'my_debug';
  149. $module = new Module($moduleID);
  150. $module->allowedIPs = ['*'];
  151. Yii::$app->setModule($moduleID, $module);
  152. $module->bootstrap(Yii::$app);
  153. Yii::getLogger()->dispatcher = $this->getMockBuilder('yii\\log\\Dispatcher')
  154. ->setMethods(['dispatch'])
  155. ->getMock();
  156. $view = Yii::$app->view;
  157. ob_start();
  158. $module->renderToolbar(new Event(['sender' => $view]));
  159. $output = ob_get_clean();
  160. $this->assertThat($output, $this->logicalOr(
  161. $this->matches('%Adata-url="/my_debug%A'),
  162. $this->matches('%Adata-url="/index.php?r=my_debug%A')
  163. ));
  164. }
  165. public function testDefaultVersion()
  166. {
  167. Yii::$app->extensions['yiisoft/yii2-debug'] = [
  168. 'name' => 'yiisoft/yii2-debug',
  169. 'version' => '2.0.7',
  170. ];
  171. $module = new Module('debug');
  172. $this->assertEquals('2.0.7', $module->getVersion());
  173. }
  174. }