ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
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  $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  public function testDataTableColumnLinkListingFormat(): void
200  {
201  $col = new Column\LinkListing($this->lng, 'col');
202  $link = new Link\Standard('label', '#');
203  $linklisting = new Listing\Unordered([$link, $link, $link]);
204  $this->assertEquals($linklisting, $col->format($linklisting));
205  }
206 
208  {
209  $this->expectException(\InvalidArgumentException::class);
210  $col = new Column\LinkListing($this->lng, 'col');
211  $linklisting_invalid = new Link\Standard('label', '#');
212  $this->assertEquals($linklisting_invalid, $col->format($linklisting_invalid));
213  }
214 
216  {
217  $this->expectException(\InvalidArgumentException::class);
218  $col = new Column\LinkListing($this->lng, 'col');
219  $link = 'some string';
220  $linklisting_invalid = new Listing\Unordered([$link, $link, $link]);
221  $this->assertEquals($linklisting_invalid, $col->format($linklisting_invalid));
222  }
223 
225  {
226  $col = (new Column\LinkListing($this->lng, 'col'))
227  ->withIsSortable(true)
228  ->withOrderingLabels(
229  'custom label ASC',
230  'custom label DESC',
231  );
232  $this->assertEquals(
233  [
234  'custom label ASC',
235  'custom label DESC'
236  ],
237  $col->getOrderingLabels()
238  );
239  }
240 
241 }
static provideColumnFormats()
Definition: ColumnTest.php:134
$lng
testDataTableColumnBoolFormatWithGlyph()
Definition: ColumnTest.php:90
testDataTableColumnAllowedFormats(Column\Column $column, $value, bool $ok)
 * provideColumnFormats 
Definition: ColumnTest.php:188
testDataTableColumnsAttributes()
Definition: ColumnTest.php:46
testDataTableColumnCustomOrderingLabels()
Definition: ColumnTest.php:224
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
Provides common functionality for UI tests.
Definition: Base.php:298
testDataTableColumnDateFormat()
Definition: ColumnTest.php:99
testDataTableColumnLinkListingFormat()
Definition: ColumnTest.php:199
$format
Definition: metadata.php:235
testDataTableColumnTimespanFormat()
Definition: ColumnTest.php:108
testDataTableColumnLinkListingItemsFormatAcceptsOnlyLinks()
Definition: ColumnTest.php:215
testDataTableColumnBoolFormat()
Definition: ColumnTest.php:74
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(Container $dic, ilPlugin $plugin)
A Column describes the form of presentation for a certain aspect of data, i.e.
Definition: Column.php:27
testDataTableColumnLinkListingFormatAcceptsOnlyLinkListings()
Definition: ColumnTest.php:207
Basic Tests for Table-Columns.
Definition: ColumnTest.php:35