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