ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
ProvideDocumentTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
45 use ilLanguage;
46 use stdClass;
47 use ilObjUser;
48 
49 require_once __DIR__ . '/../ContainerMock.php';
50 
51 class ProvideDocumentTest extends TestCase
52 {
53  use ContainerMock;
54 
55  public function testConstruct(): void
56  {
57  $this->assertInstanceOf(ProvideDocument::class, new ProvideDocument(
58  'foo',
59  $this->mock(DocumentRepository::class),
60  new SelectionMap(),
61  [],
62  $this->mock(Container::class)
63  ));
64  }
65 
66  public function testTableReadOnly(): void
67  {
68  $dummy_gui = new stdClass();
69  $legacy = $this->mock(Legacy::class);
70 
71  $container = $this->mockTree(Container::class, [
72  'ui' => [
73  'factory' => $this->mockMethod(UIFactory::class, 'legacy', ['table html'], $legacy),
74  'mainTemplate' => $this->mock(ilGlobalTemplateInterface::class),
75  ],
76  'language' => $this->mock(ilLanguage::class),
77  ]);
78 
79  $table = $this->mockMethod(LegacyTable::class, 'getHTML', [], 'table html');
80  $create_table_gui = function ($gui, string $command, TableInterface $t) use ($dummy_gui, $table): LegacyTable {
81  $this->assertSame($dummy_gui, $gui);
82  $this->assertSame('dummy command', $command);
83  $this->assertInstanceOf(DocumentTable::class, $t);
84  return $table;
85  };
86 
87  $instance = new ProvideDocument('foo', $this->mock(DocumentRepository::class), new SelectionMap(), [], $container, $create_table_gui);
88  $this->assertSame($legacy, $instance->table($dummy_gui, 'dummy command'));
89  }
90 
91  public function testTableEditable(): void
92  {
93  $dummy_gui = new stdClass();
94  $legacy = $this->mock(Legacy::class);
95 
96  $container = $this->mockTree(Container::class, [
97  'ui' => [
98  'factory' => $this->mockMethod(UIFactory::class, 'legacy', ['table html'], $legacy),
99  'mainTemplate' => $this->mock(ilGlobalTemplateInterface::class),
100  ],
101  'language' => $this->mock(ilLanguage::class),
102  ]);
103 
104  $table = $this->mockMethod(LegacyTable::class, 'getHTML', [], 'table html');
105  $create_table_gui = function ($gui, string $command, TableInterface $t) use ($dummy_gui, $table): LegacyTable {
106  $this->assertSame($dummy_gui, $gui);
107  $this->assertSame('dummy command', $command);
108  $this->assertInstanceOf(EditableDocumentTable::class, $t);
109  return $table;
110  };
111 
112  $instance = new ProvideDocument('foo', $this->mock(DocumentRepository::class), new SelectionMap(), [], $container, $create_table_gui);
113  $this->assertSame($legacy, $instance->table($dummy_gui, 'dummy command', $this->mock(EditLinks::class)));
114  }
115 
116  public function testChooseDocumentFor(): void
117  {
118  $document = $this->mockMethod(Document::class, 'criteria', [], []);
119  $user = $this->mock(ilObjUser::class);
120 
121  $criterion = $this->mockTree(Criterion::class, ['content' => ['type' => 'bar']]);
122 
123  $repository = $this->mockMethod(DocumentRepository::class, 'all', [], [
124  $this->mockMethod(Document::class, 'criteria', [], [$criterion]),
125  $document,
126  ]);
127 
128  $instance = new ProvideDocument('foo', $repository, new SelectionMap([
129  'bar' => $this->mockMethod(ConditionDefinition::class, 'withCriterion', [$criterion->content()], $this->mockMethod(
130  Condition::class,
131  'eval',
132  [$user],
133  false
134  )),
135  ]), [], $this->mock(Container::class));
136  $result = $instance->chooseDocumentFor($user);
137  $this->assertTrue($result->isOk());
138  $this->assertSame($document, $result->value());
139  }
140 
141  public function testDocumentMatches(): void
142  {
143  $criterion = $this->mockTree(Criterion::class, ['content' => ['type' => 'bar']]);
144  $document = $this->mockMethod(Document::class, 'criteria', [], [$criterion]);
145  $user = $this->mock(ilObjUser::class);
146 
147  $instance = new ProvideDocument('doo', $this->mock(DocumentRepository::class), new SelectionMap([
148  'bar' => $this->mockMethod(ConditionDefinition::class, 'withCriterion', [$criterion->content()], $this->mockMethod(
149  Condition::class,
150  'eval',
151  [$user],
152  true
153  )),
154  ]), [], $this->mock(Container::class));
155 
156  $this->assertTrue($instance->documentMatches($document, $user));
157  }
158 
159  public function testRepository(): void
160  {
161  $repository = $this->mock(DocumentRepository::class);
162  $instance = new ProvideDocument('foo', $repository, new SelectionMap(), [], $this->mock(Container::class));
163  $this->assertSame($repository, $instance->repository());
164  }
165 
166  public function testHash(): void
167  {
168  $instance = new ProvideDocument('foo', $this->mock(DocumentRepository::class), new SelectionMap(), [], $this->mock(Container::class));
169  $hash = $instance->hash();
170  $this->assertTrue(is_string($hash));
171  $this->assertSame(254, strlen($hash));
172  }
173 
174  public function testToCondition(): void
175  {
176  $condition = $this->mock(Condition::class);
177  $content = $this->mockMethod(CriterionContent::class, 'type', [], 'bar');
178 
179  $instance = new ProvideDocument('foo', $this->mock(DocumentRepository::class), new SelectionMap([
180  'bar' => $this->mockMethod(ConditionDefinition::class, 'withCriterion', [$content], $condition),
181  ]), [], $this->mock(Container::class));
182 
183  $this->assertSame($condition, $instance->toCondition($content));
184  }
185 
186  public function testConditionGroupsWithoutContent(): void
187  {
188  $group = $this->mock(Group::class);
189 
190  $instance = new ProvideDocument('foo', $this->mock(DocumentRepository::class), new SelectionMap([
191  'bar' => $this->mockMethod(ConditionDefinition::class, 'formGroup', [[]], $group),
192  'baz' => $this->mockMethod(ConditionDefinition::class, 'formGroup', [[]], $group),
193  ]), [], $this->mock(Container::class));
194 
195  $this->assertSame(['bar' => $group, 'baz' => $group], $instance->conditionGroups()->choices());
196  }
197 
198  public function testConditionGroups(): void
199  {
200  $group = $this->mock(Group::class);
201  $content = $this->mockTree(CriterionContent::class, [
202  'type' => 'baz',
203  'arguments' => ['a', 'b', 'c'],
204  ]);
205 
206  $instance = new ProvideDocument('foo', $this->mock(DocumentRepository::class), new SelectionMap([
207  'bar' => $this->mockMethod(ConditionDefinition::class, 'formGroup', [[]], $group),
208  'baz' => $this->mockMethod(ConditionDefinition::class, 'formGroup', [$content->arguments()], $group),
209  ]), [], $this->mock(Container::class));
210 
211  $this->assertSame(['bar' => $group, 'baz' => $group], $instance->conditionGroups($content)->choices());
212  }
213 
214  public function testValidateCriteriaContent(): void
215  {
216  $content = new CriterionContent('foo', ['bar']);
217  $existing_content = new CriterionContent('hoo', ['haz']);
218  $other_existing_content = new CriterionContent('foo', ['baz']);
219 
220  $condition = $this->mock(Condition::class);
221  $condition->expects(self::exactly(2))->method('knownToNeverMatchWith')->with($condition)->willReturn(false);
222 
223  $definition = $this->mock(ConditionDefinition::class);
224  $consecutive = [$content, $other_existing_content, $content];
225  $definition->expects(self::exactly(3))->method('withCriterion')->with(
226  $this->callback(function ($value) use (&$consecutive) {
227  $this->assertSame(array_shift($consecutive), $value);
228  return true;
229  })
230  )->willReturn($condition);
231 
232  $instance = new ProvideDocument('foo', $this->mock(DocumentRepository::class), new SelectionMap([
233  'hoo' => $this->mockMethod(ConditionDefinition::class, 'withCriterion', [$existing_content], $condition),
234  'foo' => $definition,
235  ]), [], $this->mock(Container::class));
236 
237  $result = $instance->validateCriteriaContent([
238  $this->mockTree(Criterion::class, ['content' => $existing_content]),
239  $this->mockTree(Criterion::class, ['content' => $other_existing_content]),
240  ], $content);
241  $this->assertTrue($result->isOk());
242  $this->assertSame($content, $result->value());
243  }
244 
245  public function testDuplicateCriteriaContent(): void
246  {
247  $content = new CriterionContent('foo', ['bar']);
248 
249  $instance = new ProvideDocument('foo', $this->mock(DocumentRepository::class), new SelectionMap(), [], $this->mock(Container::class));
250  $result = $instance->validateCriteriaContent([
251  $this->mockTree(Criterion::class, ['content' => new CriterionContent('foo', ['bar'])])
252  ], $content);
253  $this->assertFalse($result->isOk());
254  $this->assertSame(ProvideDocument::CRITERION_ALREADY_EXISTS, $result->error());
255  }
256 
257  public function testNeverMatchingCriteriaContent(): void
258  {
259  $content = new CriterionContent('foo', ['bar']);
260  $existing_content = new CriterionContent('hoo', ['haz']);
261 
262  $condition = $this->mock(Condition::class);
263  $condition->expects(self::once())->method('knownToNeverMatchWith')->with($condition)->willReturn(true);
264 
265  $instance = new ProvideDocument('foo', $this->mock(DocumentRepository::class), new SelectionMap([
266  'hoo' => $this->mockMethod(ConditionDefinition::class, 'withCriterion', [$existing_content], $condition),
267  'foo' => $this->mockMethod(ConditionDefinition::class, 'withCriterion', [$content], $condition),
268  ]), [], $this->mock(Container::class));
269 
270  $result = $instance->validateCriteriaContent([
271  $this->mockTree(Criterion::class, ['content' => $existing_content]),
272  ], $content);
273  $this->assertFalse($result->isOk());
274  $this->assertSame(ProvideDocument::CRITERION_WOULD_NEVER_MATCH, $result->error());
275  }
276 
277  public function testContentAsComponent(): void
278  {
279  $content = $this->mockTree(DocumentContent::class, ['type' => 'html']);
280  $component = $this->mock(Component::class);
281 
282  $instance = new ProvideDocument('foo', $this->mock(DocumentRepository::class), new SelectionMap(), [
283  'html' => function (DocumentContent $c) use ($content, $component): Component {
284  $this->assertSame($content, $c);
285  return $component;
286  }
287  ], $this->mock(Container::class));
288 
289  $this->assertSame($component, $instance->contentAsComponent($content));
290  }
291 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$c
Definition: deliver.php:9
$container
Definition: wac.php:13