PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Test/Case/View/Helper/BootstrapHelperTest.php

http://github.com/loadsys/twitter-bootstrap-helper
PHP | 464 lines | 394 code | 59 blank | 11 comment | 2 complexity | c207817c1b021e8476232df399cda98e MD5 | raw file
  1. <?php
  2. App::uses('Controller', 'Controller');
  3. App::uses('Helper', 'View');
  4. App::uses('AppHelper', 'View/Helper');
  5. App::uses('BootstrapHelper', 'TwitterBootstrap.View/Helper');
  6. if (!defined('FULL_BASE_URL')) {
  7. define('FULL_BASE_URL', 'http://cakephp.org');
  8. }
  9. class TestBootstrapController extends Controller {
  10. public $name = 'TestBootstrap';
  11. public $uses = null;
  12. }
  13. class TestBootstrapHelper extends BootstrapHelper {
  14. /**
  15. * expose a method as public
  16. */
  17. public function parseAttributes($options, $exclude = null, $insertBefore = ' ', $insertAfter = null) {
  18. return $this->_parseAttributes($options, $exclude, $insertBefore, $insertAfter);
  19. }
  20. /**
  21. * Get a protected attribute value
  22. */
  23. public function getAttribute($attribute) {
  24. if (!isset($this->{$attribute})) {
  25. return null;
  26. }
  27. return $this->{$attribute};
  28. }
  29. /**
  30. * Overwriting method to return a static string.
  31. */
  32. public function _flash_content($key = 'flash') {
  33. return "Flash content";
  34. }
  35. }
  36. class BootstrapHelperTest extends CakeTestCase {
  37. public $Bootstrap;
  38. public $View;
  39. public function setUp() {
  40. parent::setUp();
  41. $this->View = $this->getMock('View', array('append'), array(new TestBootstrapController()));
  42. $this->Bootstrap = new TestBootstrapHelper($this->View);
  43. Configure::write('Asset.timestamp', false);
  44. }
  45. public function tearDown() {
  46. parent::tearDown();
  47. unset($this->Bootstrap, $this->View);
  48. }
  49. public function testPageHeader() {
  50. $expected = array(
  51. array("div" => array("class" => "page-header")),
  52. array("h1" => true), "Page Header", "/h1",
  53. "/div"
  54. );
  55. $header = $this->Bootstrap->pageHeader("Page Header");
  56. $this->assertTags($header, $expected);
  57. }
  58. public function testValidLabels() {
  59. $expected = array(
  60. "span" => array("class" => "label"),
  61. "Message",
  62. "/span"
  63. );
  64. $default = $this->Bootstrap->label("Message");
  65. $this->assertTags($default, $expected);
  66. $expected = array(
  67. "span" => array("class" => "label label-success"),
  68. "Message",
  69. "/span"
  70. );
  71. $success = $this->Bootstrap->label("Message", "success");
  72. $this->assertTags($success, $expected);
  73. $expected = array(
  74. "span" => array("class" => "label label-warning"),
  75. "Message",
  76. "/span"
  77. );
  78. $warning = $this->Bootstrap->label("Message", "warning");
  79. $this->assertTags($warning, $expected);
  80. $expected = array(
  81. "span" => array("class" => "label label-important"),
  82. "Message",
  83. "/span"
  84. );
  85. $important = $this->Bootstrap->label("Message", "important");
  86. $this->assertTags($important, $expected);
  87. $expected = array(
  88. "span" => array("class" => "label label-info"),
  89. "Message",
  90. "/span"
  91. );
  92. $notice = $this->Bootstrap->label("Message", "info");
  93. $this->assertTags($notice, $expected);
  94. $expected = array(
  95. "span" => array("class" => "label label-inverse"),
  96. "Message",
  97. "/span"
  98. );
  99. $inverse = $this->Bootstrap->label("Message", "inverse");
  100. $this->assertTags($inverse, $expected);
  101. }
  102. public function testValidLabelsWithCustomClass() {
  103. $expected = array(
  104. "span" => array("class" => "label custom-class"),
  105. "Message",
  106. "/span"
  107. );
  108. $default = $this->Bootstrap->label("Message", null, array("class" => "custom-class"));
  109. $this->assertTags($default, $expected);
  110. $expected = array(
  111. "span" => array("class" => "label label-success custom-class"),
  112. "Message",
  113. "/span"
  114. );
  115. $success = $this->Bootstrap->label("Message", "success", array("class" => "custom-class"));
  116. $this->assertTags($success, $expected);
  117. $expected = array(
  118. "span" => array("class" => "label label-warning custom-class"),
  119. "Message",
  120. "/span"
  121. );
  122. $warning = $this->Bootstrap->label("Message", "warning", array("class" => "custom-class"));
  123. $this->assertTags($warning, $expected);
  124. $expected = array(
  125. "span" => array("class" => "label label-important custom-class"),
  126. "Message",
  127. "/span"
  128. );
  129. $important = $this->Bootstrap->label("Message", "important", array("class" => "custom-class"));
  130. $this->assertTags($important, $expected);
  131. $expected = array(
  132. "span" => array("class" => "label label-info custom-class"),
  133. "Message",
  134. "/span"
  135. );
  136. $notice = $this->Bootstrap->label("Message", "info", array("class" => "custom-class"));
  137. $this->assertTags($notice, $expected);
  138. $expected = array(
  139. "span" => array("class" => "label label-inverse custom-class"),
  140. "Message",
  141. "/span"
  142. );
  143. $inverse = $this->Bootstrap->label("Message", "inverse", array("class" => "custom-class"));
  144. $this->assertTags($inverse, $expected);
  145. }
  146. public function testInvalidLabel() {
  147. $expected = array("span" => array("class" => "label"), "Message", "/span");
  148. // Returns default label when passed invalid string
  149. $invalid_string = $this->Bootstrap->label("Message", "invalid");
  150. $this->assertTags($invalid_string, $expected);
  151. // Returns default label when passed invalid int
  152. $invalid_int = $this->Bootstrap->label("Message", 12);
  153. $this->assertTags($invalid_int, $expected);
  154. }
  155. public function testValidBadge() {
  156. $expected = array("span" => array("class" => "badge"), 1, "/span");
  157. $badge = $this->Bootstrap->badge(1);
  158. $this->assertTags($badge, $expected);
  159. $expected = array("span" => array("class" => "badge badge-success"), 1, "/span");
  160. $badge = $this->Bootstrap->badge(1, "success");
  161. $this->assertTags($badge, $expected);
  162. $expected = array("span" => array("class" => "badge badge-warning"), 1, "/span");
  163. $badge = $this->Bootstrap->badge(1, "warning");
  164. $this->assertTags($badge, $expected);
  165. $expected = array("span" => array("class" => "badge badge-important"), 1, "/span");
  166. $badge = $this->Bootstrap->badge(1, "important");
  167. $this->assertTags($badge, $expected);
  168. $expected = array("span" => array("class" => "badge badge-info"), 1, "/span");
  169. $badge = $this->Bootstrap->badge(1, "info");
  170. $this->assertTags($badge, $expected);
  171. $expected = array("span" => array("class" => "badge badge-inverse"), 1, "/span");
  172. $badge = $this->Bootstrap->badge(1, "inverse");
  173. $this->assertTags($badge, $expected);
  174. }
  175. public function testValidBadgeWithCustomClass() {
  176. $expected = array(
  177. "span" => array("class" => "badge custom-class"),
  178. 1,
  179. "/span"
  180. );
  181. $badge = $this->Bootstrap->badge(1, null, array("class" => "custom-class"));
  182. $this->assertTags($badge, $expected);
  183. $expected = array(
  184. "span" => array("class" => "badge badge-success custom-class"),
  185. 1,
  186. "/span"
  187. );
  188. $badge = $this->Bootstrap->badge(1, "success", array("class" => "custom-class"));
  189. $this->assertTags($badge, $expected);
  190. $expected = array(
  191. "span" => array("class" => "badge badge-warning custom-class"),
  192. 1,
  193. "/span"
  194. );
  195. $badge = $this->Bootstrap->badge(1, "warning", array("class" => "custom-class"));
  196. $this->assertTags($badge, $expected);
  197. $expected = array(
  198. "span" => array("class" => "badge badge-important custom-class"),
  199. 1,
  200. "/span"
  201. );
  202. $badge = $this->Bootstrap->badge(1, "important", array("class" => "custom-class"));
  203. $this->assertTags($badge, $expected);
  204. $expected = array(
  205. "span" => array("class" => "badge badge-info custom-class"),
  206. 1,
  207. "/span"
  208. );
  209. $badge = $this->Bootstrap->badge(1, "info", array("class" => "custom-class"));
  210. $this->assertTags($badge, $expected);
  211. $expected = array(
  212. "span" => array("class" => "badge badge-inverse custom-class"),
  213. 1,
  214. "/span"
  215. );
  216. $badge = $this->Bootstrap->badge(1, "inverse", array("class" => "custom-class"));
  217. $this->assertTags($badge, $expected);
  218. }
  219. public function testInvalidBadge() {
  220. $expected = array("span" => array("class" => "badge"), 1, "/span");
  221. $badge = $this->Bootstrap->badge(1, "invalid");
  222. $this->assertTags($badge, $expected);
  223. }
  224. public function testProgressBar() {
  225. $expected = array(
  226. array("div" => array("class" => "progress")),
  227. array("div" => array("class" => "bar", "style" => "width: 60%;")),
  228. array("/div" => true),
  229. array("/div" => true)
  230. );
  231. $progress = $this->Bootstrap->progress(array("width" => 60));
  232. $this->assertTags($progress, $expected);
  233. $expected = array(
  234. array("div" => array("class" => "progress progress-info progress-striped")),
  235. array("div" => array("class" => "bar", "style" => "width: 60%;")),
  236. array("/div" => true),
  237. array("/div" => true)
  238. );
  239. $progress = $this->Bootstrap->progress(array(
  240. "width" => 60,
  241. "style" => "info",
  242. "striped" => true
  243. ));
  244. $this->assertTags($progress, $expected);
  245. $expected = array(
  246. array("div" => array("class" => "progress progress-success progress-striped active")),
  247. array("div" => array("class" => "bar", "style" => "width: 60%;")),
  248. array("/div" => true),
  249. array("/div" => true)
  250. );
  251. $progress = $this->Bootstrap->progress(array(
  252. "width" => 60,
  253. "style" => "success",
  254. "striped" => true,
  255. "active" => true
  256. ));
  257. $this->assertTags($progress, $expected);
  258. $expected = array(
  259. array("div" => array("class" => "progress progress-warning")),
  260. array("div" => array("class" => "bar", "style" => "width: 60%;")),
  261. array("/div" => true),
  262. array("/div" => true)
  263. );
  264. $progress = $this->Bootstrap->progress(array(
  265. "width" => 60,
  266. "style" => "warning"
  267. ));
  268. $this->assertTags($progress, $expected);
  269. $expected = array(
  270. array("div" => array("class" => "progress progress-danger")),
  271. array("div" => array("class" => "bar", "style" => "width: 60%;")),
  272. array("/div" => true),
  273. array("/div" => true)
  274. );
  275. $progress = $this->Bootstrap->progress(array(
  276. "width" => 60,
  277. "style" => "danger"
  278. ));
  279. $this->assertTags($progress, $expected);
  280. }
  281. public function testIcon() {
  282. $expected = '<i class="icon-test"></i>';
  283. $result = $this->Bootstrap->icon("test");
  284. $this->assertEquals($result, $expected);
  285. $expected = '<i class="icon-test icon-white"></i>';
  286. $result = $this->Bootstrap->icon("test", "white");
  287. $this->assertEquals($result, $expected);
  288. }
  289. public function testValidFlash() {
  290. $expected = array(
  291. 'div' => array('class' => 'alert alert-warning'),
  292. 'Flash content',
  293. '/div'
  294. );
  295. $result = $this->Bootstrap->flash();
  296. $this->assertTags($result, $expected);
  297. $result = $this->Bootstrap->flash("flash");
  298. $this->assertTags($result, $expected);
  299. $result = $this->Bootstrap->flash("warning");
  300. $this->assertTags($result, $expected);
  301. $expected['div']['class'] = 'alert alert-info';
  302. $result = $this->Bootstrap->flash("info");
  303. $this->assertTags($result, $expected);
  304. $expected['div']['class'] = 'alert alert-success';
  305. $result = $this->Bootstrap->flash("success");
  306. $this->assertTags($result, $expected);
  307. $expected['div']['class'] = 'alert alert-error';
  308. $result = $this->Bootstrap->flash("error");
  309. $this->assertTags($result, $expected);
  310. $expected['div']['class'] = 'alert alert-error';
  311. $result = $this->Bootstrap->flash("auth");
  312. $this->assertTags($result, $expected);
  313. }
  314. public function testClosableFlash() {
  315. $expected = array(
  316. 'div' => array('class' => 'alert alert-warning'),
  317. 'a' => array("data-dismiss" => "alert", "class" => "close"), 'preg:/&times;/', '/a',
  318. 'Flash content',
  319. '/div'
  320. );
  321. $result = $this->Bootstrap->flash("flash", array("closable" => true));
  322. $this->assertTags($result, $expected);
  323. $expected['div']['class'] = 'alert alert-info';
  324. $result = $this->Bootstrap->flash("info", array("closable" => true));
  325. $this->assertTags($result, $expected);
  326. }
  327. public function testInvalidFlash() {
  328. $expected = array(
  329. 'div' => array('class' => 'alert alert-warning invalid'),
  330. 'Flash content',
  331. '/div'
  332. );
  333. $result = $this->Bootstrap->flash("invalid");
  334. $this->assertTags($result, $expected);
  335. }
  336. public function testFlashes() {
  337. $keys = array("info", "success", "error", "warning", "warning");
  338. $tmpl = '<div class="alert alert-%s">Flash content</div>';
  339. $expected = '';
  340. foreach ($keys as $key) {
  341. $expected .= sprintf($tmpl, $key);
  342. }
  343. $flashes = $this->Bootstrap->flashes();
  344. $this->assertEquals($flashes, $expected);
  345. $keys[] = "error";
  346. $expected = '';
  347. foreach ($keys as $key) {
  348. $expected .= sprintf($tmpl, $key);
  349. }
  350. $flashes = $this->Bootstrap->flashes(array("auth" => true));
  351. $this->assertEquals($flashes, $expected);
  352. }
  353. public function testValidBlock() {
  354. $expected = array(
  355. 'div' => array('class' => 'alert alert-block'),
  356. 'Message content',
  357. '/div'
  358. );
  359. $result = $this->Bootstrap->block("Message content");
  360. $this->assertTags($result, $expected);
  361. $expected['div']['class'] = 'alert alert-block alert-info';
  362. $result = $this->Bootstrap->block(
  363. "Message content",
  364. array("style" => "info")
  365. );
  366. $this->assertTags($result, $expected);
  367. $expected['div']['class'] = 'alert alert-block alert-success';
  368. $result = $this->Bootstrap->block(
  369. "Message content",
  370. array("style" => "success")
  371. );
  372. $this->assertTags($result, $expected);
  373. $expected['div']['class'] = 'alert alert-block alert-error';
  374. $result = $this->Bootstrap->block(
  375. "Message content",
  376. array("style" => "error")
  377. );
  378. $this->assertTags($result, $expected);
  379. $expected['div']['class'] = 'alert alert-block';
  380. $result = $this->Bootstrap->block(
  381. "Message content",
  382. array("style" => "warning")
  383. );
  384. $this->assertTags($result, $expected);
  385. $expected = array(
  386. 'div' => array('class' => 'alert alert-block'),
  387. 'h4' => array('class' => 'alert-heading'), 'Block Heading', '/h4',
  388. 'Message content',
  389. '/div'
  390. );
  391. $result = $this->Bootstrap->block(
  392. "Message content",
  393. array("heading" => "Block Heading")
  394. );
  395. $this->assertTags($result, $expected);
  396. }
  397. public function testClosableBlock() {
  398. $expected = '<div class="alert alert-block alert-info"><a class="close" data-dismiss="alert">&times;</a>Message content</div>';
  399. $result = $this->Bootstrap->block(
  400. "Message content",
  401. array("closable" => true, "style" => "info")
  402. );
  403. $this->assertEquals($result, $expected);
  404. }
  405. }