PageRenderTime 22ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/contact-form-7-to-database-extension/phpunit/ShortCodeBeforeAfterTest.php

https://gitlab.com/mattswann/launch-housing
PHP | 252 lines | 202 code | 37 blank | 13 comment | 1 complexity | a35ab46bf460b3b982c80e56a6810a82 MD5 | raw file
  1. <?php
  2. include_once(dirname(dirname(__FILE__)) . '/ExportToHtmlTemplate.php');
  3. include_once(dirname(dirname(__FILE__)) . '/ExportToHtmlTable.php');
  4. include_once(dirname(dirname(__FILE__)) . '/ExportToJson.php');
  5. include_once(dirname(dirname(__FILE__)) . '/ExportToValue.php');
  6. include_once('MockQueryResultIterator.php');
  7. include_once('WP_Mock_Functions.php');
  8. include_once('WPDB_Mock.php');
  9. include_once('SquashOutputUnitTest.php');
  10. class ShortCodeBeforeAfterTest extends SquashOutputUnitTest {
  11. var $bufferOutput = false;
  12. public function tearDown() {
  13. if ($this->bufferOutput) {
  14. ob_flush();
  15. ob_end_clean();
  16. $this->bufferOutput = false;
  17. }
  18. parent::tearDown();
  19. ob_end_clean();
  20. ob_end_clean(); // not sure why we need to call twice to suppress output
  21. }
  22. public function exportSetup($data) {
  23. date_default_timezone_set('America/New_York');
  24. $mock = new MockQueryResultIterator($data);
  25. CFDBQueryResultIteratorFactory::getInstance()->setQueryResultsIteratorMock($mock);
  26. global $wpdb;
  27. $wpdb = new WPDB_Mock;
  28. $fields = array();
  29. foreach (array_keys($data[0]) as $key) {
  30. $fields[] = (object)array('field_name' => $key);
  31. }
  32. $wpdb->getResultReturnVal = $fields;
  33. $this->bufferOutput = true;
  34. }
  35. public function dataProvider() {
  36. $data = array();
  37. // [cfdb-html]
  38. $data[] = array('[cfdb-html]: no before, no after',
  39. '<p>To: ${first-name} ${last-name}</p>',
  40. new ExportToHtmlTemplate(),
  41. array(),
  42. '<p>To: John Doe</p>' .
  43. '<p>To: Richard Roe</p>');
  44. $data[] = array('[cfdb-html]: before, no after',
  45. '{{BEFORE}}<p><b>Registered Users</b></p>{{/BEFORE}}' .
  46. '<p>To: ${first-name} ${last-name}</p>',
  47. new ExportToHtmlTemplate(),
  48. array(),
  49. '<p><b>Registered Users</b></p>' .
  50. '<p>To: John Doe</p>' .
  51. '<p>To: Richard Roe</p>'
  52. );
  53. $data[] = array('[cfdb-html]: no before, after',
  54. '<p>To: ${first-name} ${last-name}</p>' .
  55. '{{AFTER}}<p><b>Thank you!</b></p>{{/AFTER}}',
  56. new ExportToHtmlTemplate(),
  57. array(),
  58. '<p>To: John Doe</p>' .
  59. '<p>To: Richard Roe</p>' .
  60. '<p><b>Thank you!</b></p>'
  61. );
  62. $data[] = array('[cfdb-html]: before, after',
  63. '{{BEFORE}}<p><b>Registered Users</b></p>{{/BEFORE}}' .
  64. '<p>To: ${first-name} ${last-name}</p>' .
  65. '{{AFTER}}<p><b>Thank you!</b></p>{{/AFTER}}',
  66. new ExportToHtmlTemplate(),
  67. array(),
  68. '<p><b>Registered Users</b></p>' .
  69. '<p>To: John Doe</p>' .
  70. '<p>To: Richard Roe</p>' .
  71. '<p><b>Thank you!</b></p>'
  72. );
  73. // [cfdb-json]
  74. $jsonOutput = '[
  75. {"first-name":"John","last-name":"Doe"},
  76. {"first-name":"Richard","last-name":"Roe"}
  77. ]';
  78. $data[] = array('[cfdb-json]: no before, no after',
  79. '',
  80. new ExportToJson(),
  81. array(),
  82. $jsonOutput
  83. );
  84. $data[] = array('[cfdb-json]: before, no after',
  85. '{{BEFORE}}Hi!{{/BEFORE}}',
  86. new ExportToJson(),
  87. array(),
  88. 'Hi!' . $jsonOutput
  89. );
  90. $data[] = array('[cfdb-json]: no before, after',
  91. '{{AFTER}}Bye!{{/AFTER}}',
  92. new ExportToJson(),
  93. array(),
  94. $jsonOutput . 'Bye!'
  95. );
  96. $data[] = array('[cfdb-json]: before, after',
  97. '{{BEFORE}}Hi!{{/BEFORE}}{{AFTER}}Bye!{{/AFTER}}',
  98. new ExportToJson(),
  99. array(),
  100. 'Hi!' . $jsonOutput . 'Bye!'
  101. );
  102. // [cfdb-value]
  103. $valueOutput = 'John, Doe, Richard, Roe';
  104. $data[] = array('[cfdb-value]: no before, no after',
  105. '',
  106. new ExportToValue(),
  107. array(),
  108. $valueOutput
  109. );
  110. $data[] = array('[cfdb-value]: before, no after',
  111. '{{BEFORE}}Hi!{{/BEFORE}}',
  112. new ExportToValue(),
  113. array(),
  114. 'Hi!' . $valueOutput
  115. );
  116. $data[] = array('[cfdb-value]: no before, after',
  117. '{{AFTER}}Bye!{{/AFTER}}',
  118. new ExportToValue(),
  119. array(),
  120. $valueOutput . 'Bye!'
  121. );
  122. $data[] = array('[cfdb-value]: before, after',
  123. '{{BEFORE}}Hi!{{/BEFORE}}{{AFTER}}Bye!{{/AFTER}}',
  124. new ExportToValue(),
  125. array(),
  126. 'Hi!' . $valueOutput . 'Bye!'
  127. );
  128. // [cfdb-count]
  129. $valueOutput = '2';
  130. $data[] = array('[cfdb-count]: no before, no after',
  131. '',
  132. new ExportToValue(),
  133. array('function' => 'count'),
  134. $valueOutput
  135. );
  136. $data[] = array('[cfdb-count]: before, no after',
  137. '{{BEFORE}}Hi!{{/BEFORE}}',
  138. new ExportToValue(),
  139. array('function' => 'count'),
  140. 'Hi!' . $valueOutput
  141. );
  142. $data[] = array('[cfdb-count]: no before, after',
  143. '{{AFTER}}Bye!{{/AFTER}}',
  144. new ExportToValue(),
  145. array('function' => 'count'),
  146. $valueOutput . 'Bye!'
  147. );
  148. $data[] = array('[cfdb-count]: before, after',
  149. '{{BEFORE}}Hi!{{/BEFORE}}{{AFTER}}Bye!{{/AFTER}}',
  150. new ExportToValue(),
  151. array('function' => 'count'),
  152. 'Hi!' . $valueOutput . 'Bye!'
  153. );
  154. // [cfdb-table]
  155. $tableOutput = $this->getTableOutput();
  156. $data[] = array('[cfdb-table]: no before, no after',
  157. '',
  158. new ExportToHtmlTable(),
  159. array('id' => 'mytable', 'class' => 'myclass'),
  160. $tableOutput
  161. );
  162. $data[] = array('[cfdb-table]: before, no after',
  163. '{{BEFORE}}Hi!{{/BEFORE}}',
  164. new ExportToHtmlTable(),
  165. array('id' => 'mytable', 'class' => 'myclass'),
  166. 'Hi!' . $tableOutput
  167. );
  168. $data[] = array('[cfdb-table]: no before, after',
  169. '{{AFTER}}Bye!{{/AFTER}}',
  170. new ExportToHtmlTable(),
  171. array('id' => 'mytable', 'class' => 'myclass'),
  172. $tableOutput . 'Bye!'
  173. );
  174. $data[] = array('[cfdb-table]: before, after',
  175. '{{BEFORE}}Hi!{{/BEFORE}}{{AFTER}}Bye!{{/AFTER}}',
  176. new ExportToHtmlTable(),
  177. array('id' => 'mytable', 'class' => 'myclass'),
  178. 'Hi!' . $tableOutput . 'Bye!'
  179. );
  180. return $data;
  181. }
  182. /**
  183. * @dataProvider dataProvider
  184. * @param $message string error message
  185. * @param $content string inner short code content
  186. * @param $exp ExportBase subclass instance
  187. * @param $options array export code options
  188. * @param $expected string expected export output
  189. */
  190. public function test_export($message, $content, $exp, $options, $expected) {
  191. $data = array(
  192. array('first-name' => 'John', 'last-name' => 'Doe'),
  193. array('first-name' => 'Richard', 'last-name' => 'Roe')
  194. );
  195. $this->exportSetup($data);
  196. $options['content'] = $content;
  197. ob_start();
  198. $exp->export('Form', $options);
  199. $text = ob_get_contents();
  200. $this->assertEquals($expected, $text, $message);
  201. }
  202. public function getTableOutput() {
  203. $data = array(
  204. array('first-name' => 'John', 'last-name' => 'Doe'),
  205. array('first-name' => 'Richard', 'last-name' => 'Roe')
  206. );
  207. $this->exportSetup($data);
  208. $options = array('id' => 'mytable', 'class' => 'myclass');
  209. $exp = new ExportToHtmlTable();
  210. ob_start();
  211. $exp->export('Form', $options);
  212. $text = ob_get_contents();
  213. return $text;
  214. }
  215. }