ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
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 
188  #[\PHPUnit\Framework\Attributes\DataProvider('provideColumnFormats')]
190  Column\Column $column,
191  mixed $value,
192  bool $ok
193  ): void {
194  if(! $ok) {
195  $this->expectException(\InvalidArgumentException::class);
196  }
197  $this->assertEquals($value, $column->format($value));
198  }
199 
200 
201  public function testDataTableColumnLinkListingFormat(): void
202  {
203  $col = new Column\LinkListing($this->lng, 'col');
204  $link = new Link\Standard('label', '#');
205  $linklisting = new Listing\Unordered([$link, $link, $link]);
206  $this->assertEquals($linklisting, $col->format($linklisting));
207  }
208 
210  {
211  $this->expectException(\InvalidArgumentException::class);
212  $col = new Column\LinkListing($this->lng, 'col');
213  $linklisting_invalid = new Link\Standard('label', '#');
214  $this->assertEquals($linklisting_invalid, $col->format($linklisting_invalid));
215  }
216 
218  {
219  $this->expectException(\InvalidArgumentException::class);
220  $col = new Column\LinkListing($this->lng, 'col');
221  $link = 'some string';
222  $linklisting_invalid = new Listing\Unordered([$link, $link, $link]);
223  $this->assertEquals($linklisting_invalid, $col->format($linklisting_invalid));
224  }
225 
227  {
228  $col = (new Column\LinkListing($this->lng, 'col'))
229  ->withIsSortable(true)
230  ->withOrderingLabels(
231  'custom label ASC',
232  'custom label DESC',
233  );
234  $this->assertEquals(
235  [
236  'custom label ASC',
237  'custom label DESC'
238  ],
239  $col->getOrderingLabels()
240  );
241  }
242 
243 }
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:226
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:201
testDataTableColumnTimespanFormat()
Definition: ColumnTest.php:111
testDataTableColumnAllowedFormats(Column\Column $column, mixed $value, bool $ok)
Definition: ColumnTest.php:189
testDataTableColumnLinkListingItemsFormatAcceptsOnlyLinks()
Definition: ColumnTest.php:217
testDataTableColumnBoolFormat()
Definition: ColumnTest.php:77
__construct(Container $dic, ilPlugin $plugin)
testDataTableColumnLinkListingFormatAcceptsOnlyLinkListings()
Definition: ColumnTest.php:209
Basic Tests for Table-Columns.
Definition: ColumnTest.php:36