FormatHelperTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <?php
  2. namespace Tools\TestCase\View\Helper;
  3. use Tools\View\Helper\FormatHelper;
  4. use Tools\TestSuite\TestCase;
  5. use Cake\View\View;
  6. use Cake\Core\Configure;
  7. /**
  8. * Datetime Test Case
  9. */
  10. class FormatHelperTest extends TestCase {
  11. public $fixtures = ['core.sessions'];
  12. public $Format;
  13. public function setUp() {
  14. parent::setUp();
  15. Configure::write('App.imageBaseUrl', 'img/');
  16. $this->Format = new FormatHelper(new View(null));
  17. }
  18. /**
  19. * @return void
  20. */
  21. public function testDisabledLink() {
  22. $content = 'xyz';
  23. $data = [
  24. [],
  25. ['class' => 'disabledLink', 'title' => false],
  26. ['class' => 'helloClass', 'title' => 'helloTitle']
  27. ];
  28. foreach ($data as $key => $value) {
  29. $res = $this->Format->disabledLink($content, $value);
  30. //echo ''.$res.' (\''.h($res).'\')';
  31. $this->assertTrue(!empty($res));
  32. }
  33. }
  34. /**
  35. * @return void
  36. */
  37. public function testWarning() {
  38. $content = 'xyz';
  39. $data = [
  40. true,
  41. false
  42. ];
  43. foreach ($data as $key => $value) {
  44. $res = $this->Format->warning($content . ' ' . (int)$value, $value);
  45. //echo ''.$res.'';
  46. $this->assertTrue(!empty($res));
  47. }
  48. }
  49. /**
  50. * FormatHelperTest::testIcon()
  51. *
  52. * @return void
  53. */
  54. public function testIcon() {
  55. $result = $this->Format->icon('edit');
  56. $expected = '<img src="/img/icons/edit.gif" title="' . __d('tools', 'Edit') . '" alt="[' . __d('tools', 'Edit') . ']" class="icon"/>';
  57. $this->assertEquals($expected, $result);
  58. }
  59. /**
  60. * FormatHelperTest::testCIcon()
  61. *
  62. * @return void
  63. */
  64. public function testCIcon() {
  65. $result = $this->Format->cIcon('edit.png');
  66. $expected = '<img src="/img/icons/edit.png" title="' . __d('tools', 'Edit') . '" alt="[' . __d('tools', 'Edit') . ']" class="icon"/>';
  67. $this->assertEquals($expected, $result);
  68. }
  69. /**
  70. * FormatHelperTest::testIconWithFontIcon()
  71. *
  72. * @return void
  73. */
  74. public function testIconWithFontIcon() {
  75. $this->Format->config('fontIcons', ['edit' => 'fa fa-pencil']);
  76. $result = $this->Format->icon('edit');
  77. $expected = '<i class="fa fa-pencil edit" title="' . __d('tools', 'Edit') . '" data-placement="bottom" data-toggle="tooltip"></i>';
  78. $this->assertEquals($expected, $result);
  79. }
  80. /**
  81. * FormatHelperTest::testCIconWithFontIcon()
  82. *
  83. * @return void
  84. */
  85. public function testCIconWithFontIcon() {
  86. $this->Format->config('fontIcons', ['edit' => 'fa fa-pencil']);
  87. $result = $this->Format->cIcon('edit.png');
  88. $expected = '<i class="fa fa-pencil edit" title="' . __d('tools', 'Edit') . '" data-placement="bottom" data-toggle="tooltip"></i>';
  89. $this->assertEquals($expected, $result);
  90. }
  91. /**
  92. * FormatHelperTest::testSpeedOfIcons()
  93. *
  94. * @return void
  95. */
  96. public function testSpeedOfIcons() {
  97. $count = 1000;
  98. $time1 = microtime(true);
  99. for ($i = 0; $i < $count; $i++) {
  100. $result = $this->Format->icon('edit');
  101. }
  102. $time2 = microtime(true);
  103. $this->Format->config('fontIcons', ['edit' => 'fa fa-pencil']);
  104. $time3 = microtime(true);
  105. for ($i = 0; $i < $count; $i++) {
  106. $result = $this->Format->icon('edit');
  107. }
  108. $time4 = microtime(true);
  109. $normalIconSpeed = number_format($time2 - $time1, 2);
  110. $this->debug('Normal Icons: ' . $normalIconSpeed);
  111. $fontIconViaStringTemplateSpeed = number_format($time4 - $time3, 2);
  112. $this->debug('StringTemplate and Font Icons: ' . $fontIconViaStringTemplateSpeed);
  113. $this->assertTrue($fontIconViaStringTemplateSpeed < $normalIconSpeed);
  114. }
  115. /**
  116. * @return void
  117. */
  118. public function testFontIcon() {
  119. $result = $this->Format->fontIcon('signin');
  120. $expected = '<i class="fa fa-signin"></i>';
  121. $this->assertEquals($expected, $result);
  122. $result = $this->Format->fontIcon('signin', ['rotate' => 90]);
  123. $expected = '<i class="fa fa-signin fa-rotate-90"></i>';
  124. $this->assertEquals($expected, $result);
  125. $result = $this->Format->fontIcon('signin', ['size' => 5, 'extra' => ['muted']]);
  126. $expected = '<i class="fa fa-signin fa-muted fa-5x"></i>';
  127. $this->assertEquals($expected, $result);
  128. $result = $this->Format->fontIcon('signin', ['size' => 5, 'extra' => ['muted'], 'namespace' => 'icon']);
  129. $expected = '<i class="icon icon-signin icon-muted icon-5x"></i>';
  130. $this->assertEquals($expected, $result);
  131. }
  132. /**
  133. * @return void
  134. */
  135. public function testYesNo() {
  136. $result = $this->Format->yesNo(true);
  137. $expected = '<img src="/img/icons/yes.gif" title="' . __d('tools', 'Yes') . '" alt=';
  138. $this->assertTextContains($expected, $result);
  139. $result = $this->Format->yesNo(false);
  140. $expected = '<img src="/img/icons/no.gif" title="' . __d('tools', 'No') . '" alt=';
  141. $this->assertTextContains($expected, $result);
  142. $this->Format->config('fontIcons', [
  143. 'yes' => 'fa fa-check',
  144. 'no' => 'fa fa-times']);
  145. $result = $this->Format->yesNo(true);
  146. $expected = '<i class="fa fa-check yes" title="' . __d('tools', 'Yes') . '" data-placement="bottom" data-toggle="tooltip"></i>';
  147. $this->assertEquals($expected, $result);
  148. $result = $this->Format->yesNo(false);
  149. $expected = '<i class="fa fa-times no" title="' . __d('tools', 'No') . '" data-placement="bottom" data-toggle="tooltip"></i>';
  150. $this->assertEquals($expected, $result);
  151. }
  152. /**
  153. * @return void
  154. */
  155. public function testOk() {
  156. $content = 'xyz';
  157. $data = [
  158. true,
  159. false
  160. ];
  161. foreach ($data as $key => $value) {
  162. $res = $this->Format->ok($content . ' ' . (int)$value, $value);
  163. //echo ''.$res.'';
  164. $this->assertTrue(!empty($res));
  165. }
  166. }
  167. /**
  168. * FormatHelperTest::testThumbs()
  169. *
  170. * @return void
  171. */
  172. public function testThumbs() {
  173. $result = $this->Format->thumbs(1);
  174. $this->assertNotEmpty($result);
  175. }
  176. /**
  177. * FormatHelperTest::testGenderIcon()
  178. *
  179. * @return void
  180. */
  181. public function testGenderIcon() {
  182. $result = $this->Format->genderIcon();
  183. $this->assertNotEmpty($result);
  184. }
  185. /**
  186. * FormatHelperTest::testPad()
  187. *
  188. * @return void
  189. */
  190. public function testPad() {
  191. $result = $this->Format->pad('foo bar', 20, '-');
  192. $expected = 'foo bar-------------';
  193. $this->assertEquals($expected, $result);
  194. $result = $this->Format->pad('foo bar', 20, '-', STR_PAD_LEFT);
  195. $expected = '-------------foo bar';
  196. $this->assertEquals($expected, $result);
  197. }
  198. /**
  199. * FormatHelperTest::testAbsolutePaginateCount()
  200. *
  201. * @return void
  202. */
  203. public function testAbsolutePaginateCount() {
  204. $paginator = [
  205. 'page' => 1,
  206. 'pageCount' => 3,
  207. 'count' => 25,
  208. 'limit' => 10
  209. ];
  210. $result = $this->Format->absolutePaginateCount($paginator, 2);
  211. $this->debug($result);
  212. $this->assertEquals(2, $result);
  213. }
  214. /**
  215. * FormatHelperTest::testSiteIcon()
  216. *
  217. * @return void
  218. */
  219. public function testSiteIcon() {
  220. $result = $this->Format->siteIcon('http://www.example.org');
  221. $this->debug($result);
  222. $expected = '<img src="http://www.google.com/s2/favicons?domain=www.example.org';
  223. $this->assertContains($expected, $result);
  224. }
  225. /**
  226. * FormatHelperTest::testConfigure()
  227. *
  228. * @return void
  229. */
  230. public function testNeighbors() {
  231. if (!defined('ICON_PREV')) {
  232. define('ICON_PREV', 'prev');
  233. }
  234. if (!defined('ICON_NEXT')) {
  235. define('ICON_NEXT', 'next');
  236. }
  237. $neighbors = [
  238. 'prev' => ['ModelName' => ['id' => 1, 'foo' => 'bar']],
  239. 'next' => ['ModelName' => ['id' => 2, 'foo' => 'y']],
  240. ];
  241. $result = $this->Format->neighbors($neighbors, 'foo');
  242. $expected = '<div class="next-prev-navi nextPrevNavi"><a href="/index/1" title="bar"><img src="/img/icons/prev" alt="" class="icon"/>&nbsp;prevRecord</a>&nbsp;&nbsp;<a href="/index/2" title="y"><img src="/img/icons/next" alt="" class="icon"/>&nbsp;nextRecord</a></div>';
  243. $this->assertEquals($expected, $result);
  244. $this->Format->config('fontIcons', [
  245. 'prev' => 'fa fa-prev',
  246. 'next' => 'fa fa-next']);
  247. $result = $this->Format->neighbors($neighbors, 'foo');
  248. $expected = '<div class="next-prev-navi nextPrevNavi"><a href="/index/1" title="bar"><i class="fa fa-prev prev" title="" data-placement="bottom" data-toggle="tooltip"></i>&nbsp;prevRecord</a>&nbsp;&nbsp;<a href="/index/2" title="y"><i class="fa fa-next next" title="" data-placement="bottom" data-toggle="tooltip"></i>&nbsp;nextRecord</a></div>';
  249. $this->assertEquals($expected, $result);
  250. }
  251. /**
  252. * FormatHelperTest::testTab2space()
  253. *
  254. * @return void
  255. */
  256. public function testTab2space() {
  257. $text = "foo\t\tfoobar\tbla\n";
  258. $text .= "fooo\t\tbar\t\tbla\n";
  259. $text .= "foooo\t\tbar\t\tbla\n";
  260. $result = $this->Format->tab2space($text);
  261. //echo "<pre>" . $text . "</pre>";
  262. //echo'becomes';
  263. //echo "<pre>" . $result . "</pre>";
  264. }
  265. /**
  266. * FormatHelperTest::testArray2table()
  267. *
  268. * @return void
  269. */
  270. public function testArray2table() {
  271. $array = [
  272. ['x' => '0', 'y' => '0.5', 'z' => '0.9'],
  273. ['1', '2', '3'],
  274. ['4', '5', '6'],
  275. ];
  276. $is = $this->Format->array2table($array);
  277. //echo $is;
  278. //$this->assertEquals($expected, $is);
  279. // recursive?
  280. $array = [
  281. ['a' => ['2'], 'b' => ['2'], 'c' => ['2']],
  282. [['2'], ['2'], ['2']],
  283. [['2'], ['2'], [['s' => '3', 't' => '4']]],
  284. ];
  285. $is = $this->Format->array2table($array, ['recursive' => true]);
  286. //echo $is;
  287. }
  288. public function tearDown() {
  289. parent::tearDown();
  290. unset($this->Format);
  291. }
  292. }