ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
ColumnTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 
22 require_once("vendor/composer/vendor/autoload.php");
23 require_once(__DIR__ . "/../../../Base.php");
24 
32 
37 {
38  protected Language $lng;
39 
40  public function setUp(): void
41  {
42  $lng = $this->getMockBuilder(\ILIAS\Language\Language::class)
43  ->disableOriginalConstructor()
44  ->getMock();
45  $lng->method('txt')->willReturnCallback(fn($v) => $v);
46  $this->lng = $lng;
47  }
48 
49  public function testDataTableColumnsAttributes(): void
50  {
51  $col = new Column\Text($this->lng, 'col');
52  $this->assertEquals('col', $col->getTitle());
53 
54  $this->assertTrue($col->isSortable());
55  $this->assertFalse($col->withIsSortable(false)->isSortable());
56  $this->assertTrue($col->withIsSortable(true)->isSortable());
57  $this->assertEquals([
58  'col, order_option_alphabetical_ascending',
59  'col, order_option_alphabetical_descending',
60  ], $col->getOrderingLabels());
61 
62  $this->assertFalse($col->isOptional());
63  $this->assertTrue($col->withIsOptional(true)->isOptional());
64  $this->assertFalse($col->withIsOptional(false)->isOptional());
65 
66  $this->assertTrue($col->isInitiallyVisible());
67  $this->assertFalse($col->withIsOptional(true, false)->isInitiallyVisible());
68  $this->assertTrue($col->withIsOptional(true, true)->isInitiallyVisible());
69 
70  $this->assertFalse($col->isHighlighted());
71  $this->assertTrue($col->withHighlight(true)->isHighlighted());
72  $this->assertFalse($col->withHighlight(false)->isHighlighted());
73 
74  $this->assertEquals(12, $col->withIndex(12)->getIndex());
75  }
76 
77  public function testDataTableColumnBoolFormat(): void
78  {
79  $col = new Column\Boolean($this->lng, 'col', 'TRUE', 'FALSE');
80  $this->assertEquals('TRUE', $col->format(true));
81  $this->assertEquals('FALSE', $col->format(false));
82  }
83 
84  public function testDataTableColumnBoolFormatWithIcon(): void
85  {
86  $ok = new StandardIcon('', 'ok', 'small', false);
87  $no = new StandardIcon('', 'notok', 'small', false);
88  $col = new Column\Boolean($this->lng, 'col', $ok, $no);
89  $this->assertEquals($ok, $col->format(true));
90  $this->assertEquals($no, $col->format(false));
91  }
92 
94  {
95  $ok = new Glyph(GlyphInterface::LIKE, '');
96  $no = new Glyph(GlyphInterface::DISLIKE, '');
97  $col = new Column\Boolean($this->lng, 'col', $ok, $no);
98  $this->assertEquals($ok, $col->format(true));
99  $this->assertEquals($no, $col->format(false));
100  }
101 
102  public function testDataTableColumnDateFormat(): void
103  {
104  $df = new \ILIAS\Data\Factory();
105  $format = $df->dateFormat()->germanShort();
106  $dat = new \DateTimeImmutable();
107  $col = new Column\Date($this->lng, 'col', $format);
108  $this->assertEquals($dat->format($format->toString()), $col->format($dat));
109  }
110 
111  public function testDataTableColumnTimespanFormat(): void
112  {
113  $df = new \ILIAS\Data\Factory();
114  $format = $df->dateFormat()->germanShort();
115  $dat = new \DateTimeImmutable();
116  $col = new Column\TimeSpan($this->lng, 'col', $format);
117  $this->assertEquals(
118  $dat->format($format->toString()) . ' - ' . $dat->format($format->toString()),
119  $col->format([$dat, $dat])
120  );
121  }
122 
123  public function testDataTableColumnNumnberFormat(): void
124  {
125  $df = new \ILIAS\Data\Factory();
126  $dat = new \DateTimeImmutable();
127  $col = new Column\Number($this->lng, 'col');
128  $this->assertEquals('1', $col->format(1));
129  $col = $col->withDecimals(3);
130  $this->assertEquals('1,000', $col->format(1));
131  $col = $col->withDecimals(2)->withUnit('$', $col::UNIT_POSITION_FORE);
132  $this->assertEquals('$ 1,00', $col->format(1));
133  $col = $col->withUnit('€', $col::UNIT_POSITION_AFT);
134  $this->assertEquals('1,00 €', $col->format(1));
135  }
136 
137  public static function provideColumnFormats(): array
138  {
139  $lng = new class () extends \ilLanguage {
140  public function __construct()
141  {
142  }
143  };
144  return [
145  [
146  'column' => new Column\LinkListing($lng, ''),
147  'value' => new Listing\Unordered([(new Link\Standard('label', '#')),(new Link\Standard('label', '#'))]),
148  'ok' => true
149  ],
150  [
151  'column' => new Column\LinkListing($lng, ''),
152  'value' => new Listing\Unordered(['string', 'string']),
153  'ok' => false
154  ],
155  [
156  'column' => new Column\LinkListing($lng, ''),
157  'value' => new Listing\Ordered([(new Link\Standard('label', '#')),(new Link\Standard('label', '#'))]),
158  'ok' => true
159  ],
160  [
161  'column' => new Column\LinkListing($lng, ''),
162  'value' => 123,
163  'ok' => false
164  ],
165  [
166  'column' => new Column\Link($lng, ''),
167  'value' => new Link\Standard('label', '#'),
168  'ok' => true
169  ],
170  [
171  'column' => new Column\Link($lng, ''),
172  'value' => 'some string',
173  'ok' => false
174  ],
175  [
176  'column' => new Column\StatusIcon($lng, ''),
177  'value' => new StandardIcon('', '', 'small', false),
178  'ok' => true
179  ],
180  [
181  'column' => new Column\StatusIcon($lng, ''),
182  'value' => 'some string',
183  'ok' => false
184  ],
185  ];
186  }
187 
192  Column\Column $column,
193  mixed $value,
194  bool $ok
195  ): void {
196  if(! $ok) {
197  $this->expectException(\InvalidArgumentException::class);
198  }
199  $this->assertEquals($value, $column->format($value));
200  }
201 
202 
203  public function testDataTableColumnLinkListingFormat(): void
204  {
205  $col = new Column\LinkListing($this->lng, 'col');
206  $link = new Link\Standard('label', '#');
207  $linklisting = new Listing\Unordered([$link, $link, $link]);
208  $this->assertEquals($linklisting, $col->format($linklisting));
209  }
210 
212  {
213  $this->expectException(\InvalidArgumentException::class);
214  $col = new Column\LinkListing($this->lng, 'col');
215  $linklisting_invalid = new Link\Standard('label', '#');
216  $this->assertEquals($linklisting_invalid, $col->format($linklisting_invalid));
217  }
218 
220  {
221  $this->expectException(\InvalidArgumentException::class);
222  $col = new Column\LinkListing($this->lng, 'col');
223  $link = 'some string';
224  $linklisting_invalid = new Listing\Unordered([$link, $link, $link]);
225  $this->assertEquals($linklisting_invalid, $col->format($linklisting_invalid));
226  }
227 
229  {
230  $col = (new Column\LinkListing($this->lng, 'col'))
231  ->withIsSortable(true)
232  ->withOrderingLabels(
233  'custom label ASC',
234  'custom label DESC',
235  );
236  $this->assertEquals(
237  [
238  'custom label ASC',
239  'custom label DESC'
240  ],
241  $col->getOrderingLabels()
242  );
243  }
244 
245 }
static provideColumnFormats()
Definition: ColumnTest.php:137
testDataTableColumnBoolFormatWithGlyph()
Definition: ColumnTest.php:93
Interface Observer Contains several chained tasks and infos about them.
testDataTableColumnsAttributes()
Definition: ColumnTest.php:49
testDataTableColumnCustomOrderingLabels()
Definition: ColumnTest.php:228
testDataTableColumnNumnberFormat()
Definition: ColumnTest.php:123
Language $lng
Definition: ColumnTest.php:38
testDataTableColumnBoolFormatWithIcon()
Definition: ColumnTest.php:84
testDataTableColumnDateFormat()
Definition: ColumnTest.php:102
testDataTableColumnLinkListingFormat()
Definition: ColumnTest.php:203
testDataTableColumnTimespanFormat()
Definition: ColumnTest.php:111
testDataTableColumnAllowedFormats(Column\Column $column, mixed $value, bool $ok)
 * provideColumnFormats 
Definition: ColumnTest.php:191
testDataTableColumnLinkListingItemsFormatAcceptsOnlyLinks()
Definition: ColumnTest.php:219
testDataTableColumnBoolFormat()
Definition: ColumnTest.php:77
__construct(Container $dic, ilPlugin $plugin)
testDataTableColumnLinkListingFormatAcceptsOnlyLinkListings()
Definition: ColumnTest.php:211
Basic Tests for Table-Columns.
Definition: ColumnTest.php:36