TransformsTest

The Contact Form DB TransformsTest class.

Defined (1)

The class is defined in the following location(s).

/phpunit/TransformsTest.php  
  1. class TransformsTest extends PHPUnit_Framework_TestCase { 
  2.  
  3. public function tearDown() { 
  4. CFDBQueryResultIteratorFactory::getInstance()->clearMock(); 
  5. $wpdb = null; 
  6. try { 
  7. ob_flush(); 
  8. ob_end_clean(); 
  9. } catch (Exception $e) { 
  10.  
  11. public function setUp() { 
  12. date_default_timezone_set('America/New_York'); 
  13. $str = file_get_contents('TransformsTest.json'); 
  14. $data = json_decode($str, true); 
  15. $mock = new MockQueryResultIterator($data); 
  16. CFDBQueryResultIteratorFactory::getInstance()->setQueryResultsIteratorMock($mock); 
  17.  
  18. global $wpdb; 
  19. $wpdb = new WPDB_Mock; 
  20.  
  21. $fields = array(); 
  22. foreach (array_keys($data[0]) as $key) { 
  23. $fields[] = (object)array('field_name' => $key); 
  24. $wpdb->getResultReturnVal = $fields; 
  25.  
  26. public function test_simple() { 
  27. $options = array(); 
  28. $exp = new ExportToCsvUtf8(); 
  29. ob_start(); 
  30. $exp->export('Ages', $options); 
  31. $text = ob_get_contents(); 
  32. $this->assertTrue(strlen($text) > 20); 
  33. $this->assertTrue(strpos($text, 'msimpson') > 0); 
  34.  
  35. public function test_transform() { 
  36. $options = array(); 
  37. $options['trans'] = 'name=strtoupper(name)'; 
  38.  
  39. $exp = new ExportToCsvUtf8(); 
  40. ob_start(); 
  41. $exp->export('Ages', $options); 
  42. $text = ob_get_contents(); 
  43.  
  44. $this->assertTrue(strlen($text) > 20); 
  45. $this->assertTrue(strpos($text, 'msimpson') > 0); 
  46. $this->assertTrue(strpos($text, 'B1') > 0); 
  47. $this->assertTrue(strpos($text, 'P2') > 0); 
  48.  
  49. public function testLexicalSortClass() { 
  50. $options = array(); 
  51. $options['trans'] = 'SortByField(misc)'; 
  52.  
  53. $exp = new ExportToJson(); 
  54. ob_start(); 
  55. $exp->export('Ages', $options); 
  56. $text = ob_get_contents(); 
  57.  
  58. $stuff = json_decode($text); 
  59. $idx = 0; 
  60. $this->assertTrue(is_array($stuff)); 
  61. $this->assertEquals('X101', $stuff[$idx++]->misc); 
  62. $this->assertEquals('X11', $stuff[$idx++]->misc); 
  63. $this->assertEquals('X8', $stuff[$idx++]->misc); 
  64. $this->assertEquals('x1', $stuff[$idx++]->misc); 
  65. $this->assertEquals('x12', $stuff[$idx++]->misc); 
  66. $this->assertEquals('x123', $stuff[$idx++]->misc); 
  67. $this->assertEquals('x2', $stuff[$idx++]->misc); 
  68. $this->assertEquals('x6', $stuff[$idx++]->misc); 
  69.  
  70. public function testNaturalSortClass() { 
  71. $options = array(); 
  72. $options['trans'] = 'NaturalSortByField(misc)'; 
  73.  
  74. $exp = new ExportToJson(); 
  75. ob_start(); 
  76. $exp->export('Ages', $options); 
  77. $text = ob_get_contents(); 
  78.  
  79. $stuff = json_decode($text); 
  80. $this->assertTrue(is_array($stuff)); 
  81. $idx = 0; 
  82. $this->assertEquals('X8', $stuff[$idx++]->misc); 
  83. $this->assertEquals('X11', $stuff[$idx++]->misc); 
  84. $this->assertEquals('X101', $stuff[$idx++]->misc); 
  85. $this->assertEquals('x1', $stuff[$idx++]->misc); 
  86. $this->assertEquals('x2', $stuff[$idx++]->misc); 
  87. $this->assertEquals('x6', $stuff[$idx++]->misc); 
  88. $this->assertEquals('x12', $stuff[$idx++]->misc); 
  89. $this->assertEquals('x123', $stuff[$idx++]->misc); 
  90.  
  91. public function testTransformThenSort() { 
  92. $options = array(); 
  93. $options['trans'] = 'misc=strtoupper(misc)&&NaturalSortByField(misc)'; 
  94.  
  95. $exp = new ExportToJson(); 
  96. ob_start(); 
  97. $exp->export('Ages', $options); 
  98. $text = ob_get_contents(); 
  99.  
  100. $stuff = json_decode($text); 
  101. $this->assertTrue(is_array($stuff)); 
  102. $idx = 0; 
  103. $this->assertEquals('X1', $stuff[$idx++]->misc); 
  104. $this->assertEquals('X2', $stuff[$idx++]->misc); 
  105. $this->assertEquals('X6', $stuff[$idx++]->misc); 
  106. $this->assertEquals('X8', $stuff[$idx++]->misc); 
  107. $this->assertEquals('X11', $stuff[$idx++]->misc); 
  108. $this->assertEquals('X12', $stuff[$idx++]->misc); 
  109. $this->assertEquals('X101', $stuff[$idx++]->misc); 
  110. $this->assertEquals('X123', $stuff[$idx++]->misc); 
  111.  
  112. public function testNoDuplicateColumnAfterTransformThenSort() { 
  113. $options = array(); 
  114. $options['trans'] = 'misc=strtoupper(misc)&&NaturalSortByField(misc)'; 
  115. $options['tlimit'] = '1'; 
  116.  
  117. $exp = new ExportToJson(); 
  118. ob_start(); 
  119. $exp->export('Ages', $options); 
  120. $text = ob_get_contents(); 
  121.  
  122. // misc should not appear twice 
  123. $this->assertEquals(1, substr_count($text, 'misc')); 
  124.  
  125. public function testSimpleStat() { 
  126. $options = array(); 
  127. $options['trans'] = 'HardCodedData'; 
  128.  
  129. $exp = new ExportToJson(); 
  130. ob_start(); 
  131. $exp->export('Ages', $options); 
  132. $text = ob_get_contents(); 
  133.  
  134. $stuff = json_decode($text); 
  135. $this->assertTrue(is_array($stuff)); 
  136. $this->assertEquals('Mike', $stuff[0]->first_name); 
  137. $this->assertEquals('Oya', $stuff[1]->first_name); 
  138.  
  139. public function test_function_on_entry() { 
  140. $options = array(); 
  141. $options['trans'] = 'upperall2'; 
  142.  
  143. $exp = new ExportToJson(); 
  144. ob_start(); 
  145. $exp->export('Ages', $options); 
  146. $text = ob_get_contents(); 
  147.  
  148. $stuff = json_decode($text); 
  149. $this->assertTrue(is_array($stuff)); 
  150. $this->assertEquals('B1', $stuff[0]->name); 
  151. $this->assertEquals('X1', $stuff[0]->misc); 
  152. $this->assertEquals('A2', $stuff[1]->name); 
  153. $this->assertEquals('X11', $stuff[1]->misc); 
  154. $this->assertEquals('A', $stuff[2]->name); 
  155. $this->assertEquals('X101', $stuff[2]->misc); 
  156.  
  157. public function test_hide_metadata() { 
  158. $options = array(); 
  159. $options['trans'] = 'name=strtoupper(name)'; 
  160.  
  161. $exp = new ExportToJson(); 
  162. ob_start(); 
  163. $exp->export('Ages', $options); 
  164. $text = ob_get_contents(); 
  165. $stuff = json_decode($text); 
  166. $this->assertTrue(is_array($stuff)); 
  167. $this->assertEquals('B1', $stuff[0]->name); 
  168.  
  169. $this->assertFalse(isset($stuff[0]->fields_with_file)); 
  170. $this->assertFalse(isset($stuff[0]->submit_time)); 
  171. $this->assertFalse(isset($stuff[0]->Submit_Time_Key)); 
  172.  
  173. public function test_hide_metadata_when_sort() { 
  174. $options = array(); 
  175. $options['trans'] = 'NaturalSortByField(name)'; 
  176.  
  177. $exp = new ExportToJson(); 
  178. ob_start(); 
  179. $exp->export('Ages', $options); 
  180. $text = ob_get_contents(); 
  181. $stuff = json_decode($text); 
  182. $this->assertTrue(is_array($stuff)); 
  183.  
  184. $this->assertFalse(isset($stuff[0]->fields_with_file)); 
  185. $this->assertFalse(isset($stuff[0]->submit_time)); 
  186. $this->assertFalse(isset($stuff[0]->Submit_Time_Key)); 
  187.  
  188. public function test_limit() { 
  189. $options = array(); 
  190. $options['trans'] = 'misc=strtoupper(misc)'; 
  191. $options['limit'] = '2'; 
  192.  
  193. $exp = new ExportToJson(); 
  194. ob_start(); 
  195. $exp->export('Ages', $options); 
  196. $text = ob_get_contents(); 
  197. $stuff = json_decode($text); 
  198. $this->assertTrue(is_array($stuff)); 
  199.  
  200. $this->assertEquals(2, count($stuff)); 
  201. $this->assertEquals('X1', $stuff[0]->misc); 
  202. $this->assertEquals('X11', $stuff[1]->misc); 
  203.  
  204. public function test_limit_range() { 
  205. $options = array(); 
  206. $options['trans'] = 'misc=strtoupper(misc)'; 
  207. $options['limit'] = '3, 2'; // 2 rows starting at row 3 
  208.  
  209. $exp = new ExportToJson(); 
  210. ob_start(); 
  211. $exp->export('Ages', $options); 
  212. $text = ob_get_contents(); 
  213. $stuff = json_decode($text); 
  214. $this->assertTrue(is_array($stuff)); 
  215.  
  216. $this->assertEquals(2, count($stuff)); 
  217. $this->assertEquals('X2', $stuff[0]->misc); 
  218. $this->assertEquals('X6', $stuff[1]->misc); 
  219.  
  220. public function test_order_by() { 
  221. $options = array(); 
  222. $options['trans'] = 'name=strtoupper(name)'; 
  223. $options['orderby'] = 'name'; 
  224.  
  225. $exp = new ExportToJson(); 
  226. ob_start(); 
  227. $exp->export('Ages', $options); 
  228. $text = ob_get_contents(); 
  229. $stuff = json_decode($text); 
  230. $this->assertTrue(is_array($stuff)); 
  231.  
  232. $this->assertEquals('A', $stuff[0]->name); 
  233. $this->assertEquals('A2', $stuff[1]->name); 
  234.  
  235. public function test_order_by_desc() { 
  236. $options = array(); 
  237. $options['trans'] = 'name=strtoupper(name)'; 
  238. $options['orderby'] = 'name DESC'; 
  239.  
  240. $exp = new ExportToJson(); 
  241. ob_start(); 
  242. $exp->export('Ages', $options); 
  243. $text = ob_get_contents(); 
  244. $stuff = json_decode($text); 
  245. $this->assertTrue(is_array($stuff)); 
  246.  
  247. $this->assertEquals('P2', $stuff[0]->name); 
  248. $this->assertEquals('P1', $stuff[1]->name); 
  249.  
  250. public function test_order_by_desc_case() { 
  251. $options = array(); 
  252. $options['trans'] = 'name=strtoupper(name)'; 
  253. $options['orderby'] = 'name desc'; 
  254.  
  255. $exp = new ExportToJson(); 
  256. ob_start(); 
  257. $exp->export('Ages', $options); 
  258. $text = ob_get_contents(); 
  259. $stuff = json_decode($text); 
  260. $this->assertTrue(is_array($stuff)); 
  261.  
  262. $this->assertEquals('P2', $stuff[0]->name); 
  263. $this->assertEquals('P1', $stuff[1]->name); 
  264.  
  265. public function test_multiple_order_by_desc() { 
  266. $options = array(); 
  267. $options['trans'] = 'name=strtoupper(name)'; 
  268. $options['orderby'] = 'Submitted Login DESC, name DESC'; 
  269.  
  270. $exp = new ExportToJson(); 
  271. ob_start(); 
  272. $exp->export('Ages', $options); 
  273. $text = ob_get_contents(); 
  274. $stuff = json_decode($text); 
  275. $this->assertTrue(is_array($stuff)); 
  276.  
  277. $this->assertEquals('P2', $stuff[0]->name); 
  278. $this->assertEquals('P1', $stuff[1]->name); 
  279.  
  280. public function test_order_by_different_fields() { 
  281. $options = array(); 
  282. $options['trans'] = 'HardCodedData'; 
  283. $options['orderby'] = 'first_name DESC'; 
  284.  
  285. $exp = new ExportToJson(); 
  286. ob_start(); 
  287. $exp->export('Ages', $options); 
  288. $text = ob_get_contents(); 
  289. $stuff = json_decode($text); 
  290. $this->assertTrue(is_array($stuff)); 
  291.  
  292. $this->assertEquals('Oya', $stuff[0]->first_name); 
  293. $this->assertEquals('Mike', $stuff[1]->first_name); 
  294.  
  295. public function test_headers() { 
  296. $options = array(); 
  297. $options['trans'] = 'HardCodedData'; 
  298. $options['headers'] = 'first_name=FIRST, last_name=LAST'; 
  299.  
  300. $exp = new ExportToJson(); 
  301. ob_start(); 
  302. $exp->export('Ages', $options); 
  303. $text = ob_get_contents(); 
  304.  
  305. $stuff = json_decode($text); 
  306. $this->assertTrue(is_array($stuff)); 
  307. $this->assertEquals('Mike', $stuff[0]->FIRST); 
  308. $this->assertEquals('Simpson', $stuff[0]->LAST); 
  309. $this->assertEquals('Oya', $stuff[1]->FIRST); 
  310. $this->assertEquals('Simpson', $stuff[1]->LAST); 
  311.  
  312. public function test_filter() { 
  313. $options = array(); 
  314. $options['trans'] = 'misc=strtoupper(misc)'; 
  315. $options['filter'] = 'misc~~/^X1/'; 
  316.  
  317. $exp = new ExportToJson(); 
  318. ob_start(); 
  319. $exp->export('Ages', $options); 
  320. $text = ob_get_contents(); 
  321. $stuff = json_decode($text); 
  322. $this->assertTrue(is_array($stuff)); 
  323.  
  324. $idx=0; 
  325. $this->assertEquals('X1', $stuff[$idx++]->misc); 
  326. $this->assertEquals('X11', $stuff[$idx++]->misc); 
  327. $this->assertEquals('X101', $stuff[$idx++]->misc); 
  328. $this->assertEquals('X123', $stuff[$idx++]->misc); 
  329. $this->assertEquals('X12', $stuff[$idx++]->misc); 
  330.  
  331. public function test_search() { 
  332. $options = array(); 
  333. $options['trans'] = 'misc=strtoupper(misc)'; 
  334. $options['search'] = 'X1'; 
  335.  
  336. $exp = new ExportToJson(); 
  337. ob_start(); 
  338. $exp->export('Ages', $options); 
  339. $text = ob_get_contents(); 
  340. $stuff = json_decode($text); 
  341. $this->assertTrue(is_array($stuff)); 
  342.  
  343. $idx=0; 
  344. $this->assertEquals('X1', $stuff[$idx++]->misc); 
  345. $this->assertEquals('X11', $stuff[$idx++]->misc); 
  346. $this->assertEquals('X101', $stuff[$idx++]->misc); 
  347. $this->assertEquals('X123', $stuff[$idx++]->misc); 
  348. $this->assertEquals('X12', $stuff[$idx++]->misc); 
  349.  
  350. public function test_XCount() { 
  351. $options = array(); 
  352. $options['trans'] = 'XCount'; 
  353.  
  354. $exp = new ExportToJson(); 
  355. ob_start(); 
  356. $exp->export('Ages', $options); 
  357. $text = ob_get_contents(); 
  358. $stuff = json_decode($text); 
  359. $this->assertTrue(is_array($stuff)); 
  360.  
  361. $this->assertEquals('X1', $stuff[0]->name); 
  362. $this->assertEquals(5, $stuff[0]->count); 
  363.  
  364. $this->assertEquals('X2', $stuff[1]->name); 
  365. $this->assertEquals(1, $stuff[1]->count); 
  366.  
  367. public function test_tfilter() { 
  368. $options = array(); 
  369. $options['trans'] = 'XCount'; 
  370. $options['tfilter'] = 'misc~~/^X1/'; 
  371.  
  372. $exp = new ExportToJson(); 
  373. ob_start(); 
  374. $exp->export('Ages', $options); 
  375. $text = ob_get_contents(); 
  376. $stuff = json_decode($text); 
  377. $this->assertTrue(is_array($stuff)); 
  378.  
  379. $this->assertEquals('X1', $stuff[0]->name); 
  380. $this->assertEquals(2, $stuff[0]->count); 
  381.  
  382. $this->assertEquals('X2', $stuff[1]->name); 
  383. $this->assertEquals(0, $stuff[1]->count); 
  384.  
  385. public function test_add_field_by_class() { 
  386. $options = array(); 
  387. $options['trans'] = 'AddField'; 
  388.  
  389. $exp = new ExportToJson(); 
  390. ob_start(); 
  391. $exp->export('Ages', $options); 
  392. $text = ob_get_contents(); 
  393. $stuff = json_decode($text); 
  394. $this->assertTrue(is_array($stuff)); 
  395.  
  396. $idx = 0; 
  397. $this->assertEquals($idx, $stuff[$idx]->index); ++$idx; 
  398. $this->assertEquals($idx, $stuff[$idx]->index); ++$idx; 
  399. $this->assertEquals($idx, $stuff[$idx]->index); ++$idx; 
  400. $this->assertEquals($idx, $stuff[$idx]->index); ++$idx; 
  401. $this->assertEquals($idx, $stuff[$idx]->index); ++$idx; 
  402.  
  403. public function test_add_field_by_assignment() { 
  404. $options = array(); 
  405. $options['trans'] = 'newfield=strtoupper(name)'; 
  406.  
  407. $exp = new ExportToJson(); 
  408. ob_start(); 
  409. $exp->export('Ages', $options); 
  410. $text = ob_get_contents(); 
  411. $stuff = json_decode($text); 
  412. $this->assertTrue(is_array($stuff)); 
  413.  
  414. $idx = 0; 
  415. $this->assertEquals('B1', $stuff[$idx]->newfield); ++$idx; 
  416. $this->assertEquals('A2', $stuff[$idx]->newfield); ++$idx; 
  417. $this->assertEquals('A', $stuff[$idx]->newfield); ++$idx; 
  418. $this->assertEquals('P1', $stuff[$idx]->newfield); ++$idx; 
  419. $this->assertEquals('P2', $stuff[$idx]->newfield); ++$idx; 
  420. $this->assertEquals('J', $stuff[$idx]->newfield); ++$idx; 
  421. $this->assertEquals('D', $stuff[$idx]->newfield); ++$idx; 
  422. $this->assertEquals('M', $stuff[$idx]->newfield); ++$idx; 
  423.  
  424. public function test_add_field_then_filter() { 
  425. $options = array(); 
  426. $options['trans'] = 'AddField'; 
  427. $options['filter'] = 'index>1'; 
  428.  
  429. $exp = new ExportToJson(); 
  430. ob_start(); 
  431. $exp->export('Ages', $options); 
  432. $text = ob_get_contents(); 
  433. $stuff = json_decode($text); 
  434. $this->assertTrue(is_array($stuff)); 
  435.  
  436. $idx = 0; 
  437. $this->assertEquals($idx+2, $stuff[$idx]->index); ++$idx; 
  438. $this->assertEquals($idx+2, $stuff[$idx]->index); ++$idx; 
  439. $this->assertEquals($idx+2, $stuff[$idx]->index); ++$idx; 
  440. $this->assertEquals($idx+2, $stuff[$idx]->index); ++$idx; 
  441. $this->assertEquals($idx+2, $stuff[$idx]->index); ++$idx; 
  442.  
  443. public function test_show() { 
  444. $options = array(); 
  445. $options['trans'] = 'misc=strtoupper(misc)'; 
  446. $options['orderby'] = 'misc DESC'; 
  447. $options['show'] = 'name, age'; 
  448.  
  449. $exp = new ExportToJson(); 
  450. ob_start(); 
  451. $exp->export('Ages', $options); 
  452. $text = ob_get_contents(); 
  453. $stuff = json_decode($text); 
  454. $this->assertTrue(is_array($stuff)); 
  455.  
  456. $this->assertFalse(isset($stuff[0]->misc)); 
  457. $this->assertFalse(isset($stuff[0]->Submitted)); 
  458. $this->assertEquals('d', $stuff[0]->name); 
  459. $this->assertEquals('.99999', $stuff[0]->age); 
  460.  
  461. public function test_show_submit_time() { 
  462. $options = array(); 
  463. $options['trans'] = 'misc=strtoupper(misc)'; 
  464. $options['orderby'] = 'misc DESC'; 
  465. $options['show'] = 'name, submit_time'; 
  466.  
  467. $exp = new ExportToJson(); 
  468. ob_start(); 
  469. $exp->export('Ages', $options); 
  470. $text = ob_get_contents(); 
  471. $stuff = json_decode($text); 
  472. $this->assertTrue(is_array($stuff)); 
  473.  
  474. $this->assertFalse(isset($stuff[0]->misc)); 
  475. $this->assertFalse(isset($stuff[0]->Submitted)); 
  476. $this->assertEquals('d', $stuff[0]->name); 
  477. $this->assertEquals('1401302985.4975', $stuff[0]->submit_time); 
  478.  
  479. public function test_hide() { 
  480. $options = array(); 
  481. $options['trans'] = 'misc=strtoupper(misc)'; 
  482. $options['orderby'] = 'misc DESC'; 
  483. $options['hide'] = 'misc, Submitted'; 
  484.  
  485. $exp = new ExportToJson(); 
  486. ob_start(); 
  487. $exp->export('Ages', $options); 
  488. $text = ob_get_contents(); 
  489. $stuff = json_decode($text); 
  490. $this->assertTrue(is_array($stuff)); 
  491.  
  492. $this->assertFalse(isset($stuff[0]->misc)); 
  493. $this->assertFalse(isset($stuff[0]->Submitted)); 
  494. $this->assertEquals('d', $stuff[0]->name); 
  495. $this->assertEquals('.99999', $stuff[0]->age); 
  496.  
  497. // todo: somehow to test random? 
  498.  
  499.