SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/tests/DocImport/docImportTest.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. use Sprout\Helpers\DocImport\DocImport;
  15. use Sprout\Helpers\Treenode;
  16.  
  17.  
  18. /**
  19. * Unit tests
  20. **/
  21. class docImportTest extends PHPUnit_Framework_TestCase
  22. {
  23.  
  24. /**
  25.   * Data for testGetHtmlBasic
  26.   **/
  27. public function htmlBasicData()
  28. {
  29. return array(
  30. array('', ''),
  31. array('<p>Hello</p>', '<p>Hello</p>'),
  32. array('<p>Hello<br/>world</p>', '<p>Hello<br>world</p>'),
  33. array('<p>Hello<br />world</p>', '<p>Hello<br>world</p>'),
  34. );
  35. }
  36.  
  37.  
  38. /**
  39.   * Basic getHtml tests
  40.   *
  41.   * @dataProvider htmlBasicData
  42.   **/
  43. public function testGetHtmlBasic($xml, $expect)
  44. {
  45. $dom = new DOMDocument();
  46. $dom->loadXML('<doc><body>' . $xml . '</body></doc>');
  47.  
  48. $got = DocImport::getHtml($dom);
  49.  
  50. $this->assertEquals($expect, $got);
  51. }
  52.  
  53.  
  54. /**
  55.   * Data for testGetHtmlImages
  56.   **/
  57. public function htmlImagesData()
  58. {
  59. return array(
  60. array('<img rel="aaa"/>', '<img src="image_aaa.jpg">'),
  61. array('<p>Hello</p><img rel="aaa"/>', '<p>Hello</p><img src="image_aaa.jpg">'),
  62. array('<img rel="aaa"/><p>Hello</p>', '<img src="image_aaa.jpg"><p>Hello</p>'),
  63. array('<img rel="aaa"/><img rel="bbb"/>', '<img src="image_aaa.jpg"><img src="image_bbb.jpg">'),
  64. array('<img rel="aaa"/><img rel="bbb"/><img rel="ccc"/>', '<img src="image_aaa.jpg"><img src="image_bbb.jpg">'),
  65.  
  66. array('<img rel="aaa" width="100" height="150"/>', '<img width="100" height="150" src="image_aaa.jpg">'),
  67. array('<img rel="aaa" width="" height=""/>', '<img src="image_aaa.jpg">'),
  68. array('<img rel="aaa" width="0" height="0"/>', '<img src="image_aaa.jpg">'),
  69.  
  70. array('<img rel="aaa" error="invalid" width="100" height="150"/>', '<img width="100" height="150" src="http://placehold.it/100x150&amp;text=invalid">'),
  71. array('<img rel="aaa" error="invalid" width="0" height="0"/>', '<img src="http://placehold.it/300x50&amp;text=invalid">'),
  72. array('<img rel="aaa" error="invalid" width="" height=""/>', '<img src="http://placehold.it/300x50&amp;text=invalid">'),
  73. array('<img rel="aaa" error="invalid"/>', '<img src="http://placehold.it/300x50&amp;text=invalid">'),
  74. );
  75. }
  76.  
  77.  
  78. /**
  79.   * Image getHtml tests
  80.   *
  81.   * @dataProvider htmlImagesData
  82.   **/
  83. public function testGetHtmlImages($xml, $expect)
  84. {
  85. $imgs = array(
  86. 'aaa' => 'image_aaa.jpg',
  87. 'bbb' => 'image_bbb.jpg',
  88. );
  89.  
  90. $dom = new DOMDocument();
  91. $dom->loadXML('<doc><body>' . $xml . '</body></doc>');
  92.  
  93. $got = DocImport::getHtml($dom, $imgs);
  94.  
  95. $this->assertEquals($expect, $got);
  96. }
  97.  
  98.  
  99. /**
  100.   * Data for testGetHtmlHeadings
  101.   **/
  102. public function htmlHeadingsData()
  103. {
  104. return array(
  105. array('<h1>aaa</h1>', '<h2>aaa</h2>'),
  106. );
  107. }
  108.  
  109. /**
  110.   * Image getHtml tests
  111.   *
  112.   * @dataProvider htmlHeadingsData
  113.   **/
  114. public function testGetHtmlHeadings($xml, $expect)
  115. {
  116. $headings = array(
  117. 1 => 2
  118. );
  119.  
  120. $dom = new DOMDocument();
  121. $dom->loadXML('<doc><body>' . $xml . '</body></doc>');
  122.  
  123. $got = DocImport::getHtml($dom, array(), $headings);
  124.  
  125. $this->assertEquals($expect, $got);
  126. }
  127.  
  128.  
  129. /**
  130.   * Test treenode building from headings
  131.   **/
  132. public function testGetHeadingsTreeBasics()
  133. {
  134. $dom = new DOMDocument();
  135.  
  136. $dom->loadXML('<doc><body><h1>Test</h1><p>Test</p></body></doc>');
  137. $tree = DocImport::getHeadingsTree($dom, 1);
  138. $this->assertTrue($tree instanceof Treenode);
  139. $this->assertTrue(count($tree->children) == 1);
  140. $this->assertTrue($tree->children[0] instanceof Treenode);
  141. $this->assertTrue($tree->children[0]['name'] == 'Test');
  142.  
  143. $dom->loadXML('<doc><body><h1>One</h1><p>Test</p><h1>Two</h1></body></doc>');
  144. $tree = DocImport::getHeadingsTree($dom, 1);
  145. $this->assertTrue(count($tree->children) == 2);
  146. $this->assertTrue($tree->children[0]['name'] == 'One');
  147. $this->assertTrue($tree->children[1]['name'] == 'Two');
  148.  
  149. $dom->loadXML('<doc><body><h1>One</h1><h1>Two</h1></body></doc>');
  150. $tree = DocImport::getHeadingsTree($dom, 1);
  151. $this->assertTrue(count($tree->children) == 2);
  152. $this->assertTrue($tree->children[0]['name'] == 'One');
  153. $this->assertTrue($tree->children[1]['name'] == 'Two');
  154.  
  155. $dom->loadXML('<doc><body><h1><b>One</b></h1><h1>Two</h1></body></doc>');
  156. $tree = DocImport::getHeadingsTree($dom, 1);
  157. $this->assertTrue(count($tree->children) == 2);
  158. $this->assertTrue($tree->children[0]['name'] == 'One');
  159. $this->assertTrue($tree->children[1]['name'] == 'Two');
  160. }
  161.  
  162.  
  163. /**
  164.   * Data for testGetHeadingsTreeThreeLevels
  165.   **/
  166. public function getHeadingsTreeThreeLevelsData()
  167. {
  168. return array(
  169. array('', array()),
  170. array('<h0>Zero</h0>', array()),
  171. array('<p>Para</p>', array()),
  172.  
  173. // H1
  174. array('<h1>One</h1>', array( 'One' => array() )),
  175. array('<h1>One1</h1><h1>One2</h1>', array( 'One1' => array(), 'One2' => array() )),
  176.  
  177. // H1 -> H2
  178. array('<h1>One1</h1><h2>Two1</h2><h1>One2</h1>', array( 'One1' => array('Two1'=>array()), 'One2' => array() )),
  179. array('<h1>One1</h1><h2>Two1</h2><h1>One2</h1><h2>Two2</h2>', array( 'One1' => array('Two1'=>array()), 'One2' => array('Two2'=>array()) )),
  180. array('<h1>One1</h1><h2>Two1</h2><h2>Two2</h2><h1>One2</h1>', array( 'One1' => array('Two1'=>array(),'Two2'=>array()), 'One2' => array() )),
  181. array('<h1>One1</h1><h2>Two1</h2><h2>Two2</h2><h1>One2</h1><h2>Two3</h2>', array( 'One1' => array('Two1'=>array(),'Two2'=>array()), 'One2' => array('Two3'=>array()) )),
  182.  
  183. // H1 -> H2 -> H3
  184. array('<h1>One1</h1><h2>Two1</h2><h3>Three1</h3><h1>One2</h1>', array( 'One1' => array('Two1'=>array('Three1'=>null)), 'One2' => array() )),
  185. array('<h1>One1</h1><h2>Two1</h2><h3>Three1</h3><h3>Three2</h3><h1>One2</h1>', array( 'One1' => array('Two1'=>array('Three1'=>null,'Three2'=>null)), 'One2' => array() )),
  186. array('<h1>One1</h1><h2>Two1</h2><h3>Three1</h3><h3>Three2</h3><h4>Four</h4><h1>One2</h1>', array( 'One1' => array('Two1'=>array('Three1'=>null,'Three2'=>null)), 'One2' => array() )),
  187.  
  188. // H1 -> H3
  189. array('<h1>One1</h1><h3>Three1</h3>', array( 'One1' => array('Three1'=>array()) )),
  190.  
  191. // H2
  192. array('<h2>Two1</h2>', array( 'Two1' => array() )),
  193. array('<h2>Two1</h2><h3>Three1</h3>', array( 'Two1' => array('Three1' => array()) )),
  194.  
  195. // H1/H2 -> H4
  196. array('<h1>One1</h1><h4>Three1</h4>', array( 'One1' => array() )),
  197. array('<h2>One1</h2><h4>Three1</h4>', array( 'One1' => array() )),
  198.  
  199. // Going up instead of down
  200. array('<h2>Two1</h2><h1>One1</h1>', array( 'Two1' => array(), 'One1' => array() )),
  201. array('<h3>Three1</h3><h2>Two1</h2><h1>One1</h1>', array( 'Three1' => array(), 'Two1' => array(), 'One1' => array() )),
  202. array('<h3>Three1</h3><h1>One1</h1>', array( 'Three1' => array(), 'One1' => array() )),
  203.  
  204. // Up then down
  205. array('<h2>Two1</h2><h1>One1</h1><h2>Two2</h2>', array( 'Two1' => array(), 'One1' => array('Two2' => array()) )),
  206. );
  207. }
  208.  
  209.  
  210. /**
  211.   * Test treenode building - three levels
  212.   * @dataProvider getHeadingsTreeThreeLevelsData
  213.   **/
  214. public function testGetHeadingsTreeThreeLevels($xml, $expect)
  215. {
  216. $dom = new DOMDocument();
  217. $dom->loadXML('<doc><body>' . $xml . '</body></doc>');
  218. $node0 = DocImport::getHeadingsTree($dom, 3);
  219. unset($dom);
  220.  
  221. $this->assertTrue($node0 instanceof Treenode);
  222. $this->assertTrue(count($node0->children) == count($expect));
  223.  
  224. $idx1 = 0;
  225. foreach ($expect as $name => $level1) {
  226. $node1 = $node0->children[$idx1];
  227. $this->assertTrue($node1 instanceof Treenode);
  228. $this->assertTrue($node1['name'] == $name);
  229. $this->assertTrue(count($node1->children) == count($level1));
  230.  
  231. $idx2 = 0;
  232. foreach ($level1 as $name => $level2) {
  233. $node2 = $node1->children[$idx2];
  234. $this->assertTrue($node2 instanceof Treenode);
  235. $this->assertTrue($node2['name'] == $name);
  236. $this->assertTrue(count($node2->children) == count($level2));
  237. $idx2++;
  238. }
  239.  
  240. $idx1++;
  241. }
  242. }
  243.  
  244.  
  245. /**
  246.   * Test treenode building - include body
  247.   **/
  248. public function testGetHeadingsTreeBody()
  249. {
  250. $dom = new DOMDocument();
  251.  
  252. $dom->loadXML('<doc><body><h1>Heading</h1><p>Body</p></body></doc>');
  253. $tree = DocImport::getHeadingsTree($dom, 1, true);
  254. $this->assertTrue($tree instanceof Treenode);
  255. $this->assertTrue(count($tree->children) == 1);
  256. $this->assertTrue($tree->children[0] instanceof Treenode);
  257. $this->assertTrue($tree->children[0]['name'] == 'Heading');
  258. $this->assertTrue($tree->children[0]['body'] == '<p>Body</p>');
  259.  
  260. $dom->loadXML('<doc><body><h1>Heading</h1><p>Body</p><p><b>Body</b></p></body></doc>');
  261. $tree = DocImport::getHeadingsTree($dom, 1, true);
  262. $this->assertTrue($tree instanceof Treenode);
  263. $this->assertTrue(count($tree->children) == 1);
  264. $this->assertTrue($tree->children[0] instanceof Treenode);
  265. $this->assertTrue($tree->children[0]['name'] == 'Heading');
  266. $this->assertTrue($tree->children[0]['body'] == '<p>Body</p><p><b>Body</b></p>');
  267.  
  268. $dom->loadXML('<doc><body><h1>One</h1><p>Body one</p><h1>Two</h1><p>Body two</p></body></doc>');
  269. $tree = DocImport::getHeadingsTree($dom, 1, true);
  270. $this->assertTrue($tree instanceof Treenode);
  271. $this->assertTrue(count($tree->children) == 2);
  272. $this->assertTrue($tree->children[0] instanceof Treenode);
  273. $this->assertTrue($tree->children[0]['name'] == 'One');
  274. $this->assertTrue($tree->children[0]['body'] == '<p>Body one</p>');
  275. $this->assertTrue($tree->children[1] instanceof Treenode);
  276. $this->assertTrue($tree->children[1]['name'] == 'Two');
  277. $this->assertTrue($tree->children[1]['body'] == '<p>Body two</p>');
  278. }
  279.  
  280. }
  281.