SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/tests/itemlistTest.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. use Sprout\Helpers\ColModifierDate;
  14. use Sprout\Helpers\ColModifierMoney;
  15. use Sprout\Helpers\I18n;
  16. use Sprout\Helpers\Itemlist;
  17.  
  18.  
  19. class itemlistTest extends PHPUnit_Framework_TestCase
  20. {
  21.  
  22. public function testStringVal() {
  23. $data = [
  24. ['key' => '*value*'],
  25. ];
  26.  
  27. $itemlist = new Itemlist();
  28. $itemlist->main_columns = [
  29. 'Key' => 'key',
  30. ];
  31. $itemlist->items = $data;
  32. $result = $itemlist->render();
  33.  
  34. $this->assertContains('*value*', $result);
  35. $this->assertNotContains('<tr class="main-list--aggregate">', $result);
  36. }
  37.  
  38. public function testColModifierVal() {
  39. $data = [
  40. ['key' => '2013-05-01'],
  41. ];
  42.  
  43. $itemlist = new Itemlist();
  44. $itemlist->main_columns = [
  45. 'Key' => [new ColModifierDate(), 'key'],
  46. ];
  47. $itemlist->items = $data;
  48. $result = $itemlist->render();
  49.  
  50. $this->assertContains('01/05/2013', $result);
  51. }
  52.  
  53. public function testClosure() {
  54. $data = [
  55. [],
  56. ];
  57.  
  58. $itemlist = new Itemlist();
  59. $itemlist->main_columns = [
  60. 'Key' => function($row){ return '*value*'; },
  61. ];
  62. $itemlist->items = $data;
  63. $result = $itemlist->render();
  64.  
  65. $this->assertContains('*value*', $result);
  66. }
  67.  
  68. public function testActions() {
  69. $itemlist = new Itemlist();
  70. $itemlist->main_columns = [
  71. 'ID' => 'id',
  72. ];
  73. $itemlist->items = [['id' => 1]];
  74. $itemlist->addAction('*aaa*', '*bbb*');
  75. $result = $itemlist->render();
  76.  
  77. $this->assertContains('*aaa*', $result);
  78. $this->assertContains('*bbb*', $result);
  79. }
  80.  
  81. public function testActionsClass() {
  82. $itemlist = new Itemlist();
  83. $itemlist->main_columns = [
  84. 'ID' => 'id',
  85. ];
  86. $itemlist->items = [['id' => 1]];
  87. $itemlist->addAction('*aaa*', '*bbb*', '*ccc*');
  88. $result = $itemlist->render();
  89.  
  90. $this->assertContains('*aaa*', $result);
  91. $this->assertContains('*bbb*', $result);
  92. $this->assertContains('*ccc*', $result);
  93. }
  94.  
  95. public function testActionsShowFunc() {
  96. $itemlist = new Itemlist();
  97. $itemlist->main_columns = [
  98. 'ID' => 'id',
  99. ];
  100. $itemlist->items = [['id' => 1]];
  101. $itemlist->addAction('*shown*', 'Shown', '', function($row){ return true; });
  102. $itemlist->addAction('*hidden*', 'Hidden', '', function($row){ return false; });
  103. $result = $itemlist->render();
  104.  
  105. $this->assertContains('*shown*', $result);
  106. $this->assertNotContains('*hidden*', $result);
  107. }
  108.  
  109. public function testAggregateTotal() {
  110. $itemlist = new Itemlist();
  111. $itemlist->main_columns = [
  112. 'Val' => 'val',
  113. ];
  114. $itemlist->addAggregateColumn('Val', 'sum');
  115. $itemlist->items = [['val' => 10], ['val' => 20]];
  116. $result = $itemlist->render();
  117. $this->assertContains('<tr class="main-list--aggregate">', $result);
  118. $this->assertContains('<td>30</td>', $result);
  119. }
  120.  
  121. public function testAggregateTotalColModifier() {
  122. I18n::init('AUS');
  123. $itemlist = new Itemlist();
  124. $itemlist->main_columns = [
  125. 'Val' => 'val',
  126. ];
  127. $itemlist->addAggregateColumn('Val', 'sum', new ColModifierMoney('AUS'));
  128. $itemlist->items = [['val' => 10], ['val' => 20]];
  129. $result = $itemlist->render();
  130. $this->assertContains('<td>$30.00</td>', $result);
  131. }
  132.  
  133. public function testAggregateCount() {
  134. $itemlist = new Itemlist();
  135. $itemlist->main_columns = [
  136. 'Val' => 'val',
  137. ];
  138. $itemlist->addAggregateColumn('Val', 'count');
  139. $itemlist->items = [['val' => 10], ['val' => 10], ['val' => 10]];
  140. $result = $itemlist->render();
  141. $this->assertContains('<td>3</td>', $result);
  142. }
  143.  
  144. public function testAggregateAvg() {
  145. $itemlist = new Itemlist();
  146. $itemlist->main_columns = [
  147. 'Val' => 'val',
  148. ];
  149. $itemlist->addAggregateColumn('Val', 'avg');
  150. $itemlist->items = [['val' => 10], ['val' => 20]];
  151. $result = $itemlist->render();
  152. $this->assertContains('<td>15</td>', $result);
  153. }
  154.  
  155. public function testAggregateValue() {
  156. $itemlist = new Itemlist();
  157. $itemlist->main_columns = [
  158. 'Val' => 'val',
  159. ];
  160. $itemlist->addAggregateValue('Val', '*agg*');
  161. $itemlist->items = [['val' => 10]];
  162. $result = $itemlist->render();
  163. $this->assertContains('<td>*agg*</td>', $result);
  164. }
  165.  
  166. }
  167.