/dmCorePlugin/test/unit/dmMarkdownLinkTest.php

https://github.com/xdade/diem · PHP · 145 lines · 127 code · 18 blank · 0 comment · 0 complexity · 57988a069b35a8dd7495ab3af243d4e4 MD5 · raw file

  1. <?php
  2. require_once(dirname(__FILE__).'/helper/dmUnitTestHelper.php');
  3. $helper = new dmUnitTestHelper();
  4. $helper->boot('front');
  5. $t = new lime_test(91);
  6. $markdown = $helper->get('markdown');
  7. dm::loadHelpers(array('Dm'));
  8. $t->comment('Create a test page');
  9. $page = dmDb::create('DmPage', array(
  10. 'module' => dmString::random(),
  11. 'action' => dmString::random(),
  12. 'name' => dmString::random(),
  13. 'slug' => dmString::random()
  14. ));
  15. $page->Node->insertAsFirstChildOf(dmDb::table('DmPage')->getTree()->fetchRoot());
  16. $t->ok($page->exists(), 'A test page has been created');
  17. $tests = array(
  18. '[basic link](%source%)' => array( // source
  19. 'basic link', // ->toText
  20. 'basic link', // ->brutalToText
  21. _link($page)->text('basic link') // ->toHtml
  22. ),
  23. '[link with id](%source% #an_id)' => array(
  24. 'link with id',
  25. 'link with id',
  26. _link($page)->text('link with id')->set('#an_id')
  27. ),
  28. '[link with classes](%source% .a_class.another_class)' => array(
  29. 'link with classes',
  30. 'link with classes',
  31. _link($page)->text('link with classes')->set('.a_class.another_class')
  32. ),
  33. '[link with id and classes](%source% #an_id.a_class.another_class)' => array(
  34. 'link with id and classes',
  35. 'link with id and classes',
  36. _link($page)->text('link with id and classes')->set('#an_id.a_class.another_class')
  37. ),
  38. 'a [basic link](%source%) and a [link with id and classes](%source% #an_id.a_class.another_class)' => array(
  39. 'a basic link and a link with id and classes',
  40. 'a basic link and a link with id and classes',
  41. sprintf('a %s and a %s',
  42. _link($page)->text('basic link'),
  43. _link($page)->text('link with id and classes')->set('#an_id.a_class.another_class')
  44. )
  45. ),
  46. '[link with title](%source% "this is a title")' => array(
  47. 'link with title',
  48. 'link with title',
  49. _link($page)->text('link with title')->title('this is a title')
  50. ),
  51. '[link with title, id and classes](%source% "this is a title" #an_id.a_class.another_class)' => array(
  52. 'link with title, id and classes',
  53. 'link with title, id and classes',
  54. _link($page)->text('link with title, id and classes')->title('this is a title')->set('#an_id.a_class.another_class')
  55. ),
  56. '[link with anchor](%source%#an_anchor)' => array(
  57. 'link with anchor',
  58. 'link with anchor',
  59. _link($page)->text('link with anchor')->anchor('#an_anchor')
  60. ),
  61. '[link with anchor, id and classes](%source%#an_anchor #an_id.a_class.another_class)' => array(
  62. 'link with anchor, id and classes',
  63. 'link with anchor, id and classes',
  64. _link($page)->text('link with anchor, id and classes')->anchor('#an_anchor')->set('#an_id.a_class.another_class')
  65. ),
  66. '[link with params](%source%?var1=val1&var2=val2)' => array(
  67. 'link with params',
  68. 'link with params',
  69. _link($page)->text('link with params')->params(array('var1' => 'val1', 'var2' => 'val2'))
  70. ),
  71. '[link with params, id and classes](%source%?var1=val1&var2=val2 #an_id.a_class.another_class)' => array(
  72. 'link with params, id and classes',
  73. 'link with params, id and classes',
  74. _link($page)->text('link with params, id and classes')
  75. ->params(array('var1' => 'val1', 'var2' => 'val2'))
  76. ->set('#an_id.a_class.another_class')
  77. ),
  78. '[link with anchor, params, id and classes](%source%#an_anchor?var1=val1&var2=val2 #an_id.a_class.another_class)' => array(
  79. 'link with anchor, params, id and classes',
  80. 'link with anchor, params, id and classes',
  81. _link($page)->text('link with anchor, params, id and classes')
  82. ->anchor('#an_anchor')
  83. ->params(array('var1' => 'val1', 'var2' => 'val2'))
  84. ->set('#an_id.a_class.another_class')
  85. ),
  86. '[link with title, anchor, params, id and classes](%source%#an_anchor?var1=val1&var2=val2 "this is a title" #an_id.a_class.another_class)' => array(
  87. 'link with title, anchor, params, id and classes',
  88. 'link with title, anchor, params, id and classes',
  89. _link($page)->text('link with title, anchor, params, id and classes')
  90. ->title('this is a title')
  91. ->anchor('#an_anchor')
  92. ->params(array('var1' => 'val1', 'var2' => 'val2'))
  93. ->set('#an_id.a_class.another_class')
  94. ),
  95. '[link with title, reversed anchor, params, id and classes](%source%?var1=val1&var2=val2#an_anchor "this is a title" #an_id.a_class.another_class)' => array(
  96. 'link with title, reversed anchor, params, id and classes',
  97. 'link with title, reversed anchor, params, id and classes',
  98. _link($page)->text('link with title, reversed anchor, params, id and classes')
  99. ->title('this is a title')
  100. ->anchor('#an_anchor')
  101. ->params(array('var1' => 'val1', 'var2' => 'val2'))
  102. ->set('#an_id.a_class.another_class')
  103. )
  104. );
  105. $absoluteUrlRoot = $helper->get('request')->getAbsoluteUrlRoot();
  106. foreach($tests as $code => $results)
  107. {
  108. $sourceId = str_replace('%source%', 'page:'.$page->id, $code);
  109. $t->comment($sourceId);
  110. $t->is($result = $markdown->toText($sourceId), $results[0], '->toText() '.$result);
  111. $t->is($result = $markdown->brutalToText($sourceId), $results[1], '->brutalToText() '.$result);
  112. $t->is($result = $markdown->toHtml($sourceId), _tag('p.dm_first_p', $results[2]), '->toHtml() '.$result);
  113. $sourcePath = str_replace('%source%', $absoluteUrlRoot.'/'.$page->slug, $code);
  114. $t->comment($sourcePath);
  115. $t->is($result = $markdown->toText($sourcePath), $results[0], '->toText() '.$result);
  116. $t->is($result = $markdown->brutalToText($sourcePath), $results[1], '->brutalToText() '.$result);
  117. $t->is($result = $markdown->toHtml($sourcePath), str_replace('href="', 'href="http://', _tag('p.dm_first_p', $results[2])), '->toHtml() '.$result);
  118. }
  119. $page->Node->delete();
  120. $source = '[link to email](mailto:test@mail.com)';
  121. $t->is($result = $markdown->toText($source), 'link to email', $result);
  122. $t->is($result = $markdown->brutalToText($source), 'link to email', $result);
  123. $t->is($result = $markdown->toHtml($source), '<p class="dm_first_p">'._link('mailto:test@mail.com')->text('link to email')->render().'</p>', $result);
  124. $source = '[DRY](http://c2.com/cgi/wiki?DontRepeatYourself)';
  125. $t->is($result = $markdown->toText($source), 'DRY', $result);
  126. $t->is($result = $markdown->brutalToText($source), 'DRY', $result);
  127. $t->is($result = $markdown->toHtml($source), '<p class="dm_first_p"><a class="link" href="http://c2.com/cgi/wiki?DontRepeatYourself">DRY</a></p>', $result);