ILIAS  trunk Revision v11.0_alpha-1702-gfd3ecb7f852
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
AdministrationTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
41 use ILIAS\Refinery\KindlyTo\Group as KindlyToGroup;
43 
44 require_once __DIR__ . '/ContainerMock.php';
45 
46 class AdministrationTest extends TestCase
47 {
48  use ContainerMock;
49 
50  public function testConstruct(): void
51  {
52  $config = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock();
53  $container = $this->getMockBuilder(Container::class)->disableOriginalConstructor()->getMock();
54  $ui = $this->getMockBuilder(UI::class)->disableOriginalConstructor()->getMock();
55 
56  $this->assertInstanceOf(Administration::class, new Administration($config, $container, $ui));
57  }
58 
59  public function testDeleteDocumentsConfirmation(): void
60  {
61  $config = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock();
62  $container = $this->getMockBuilder(Container::class)->disableOriginalConstructor()->getMock();
63  $ui = $this->mockMethod(UI::class, 'txt', ['sure_delete_documents_p'], 'translated');
64 
65  $documents = [$this->doc(9, 'First title'), $this->doc(49, 'Second title')];
66 
67  $confirmation = $this->mockMethod(
68  Confirmation::class,
69  'render',
70  [
71  'link',
72  'submitCommand',
73  'cancelCommand',
74  'translated',
75  [9 => 'First title', 49 => 'Second title']
76  ],
77  'rendered'
78  );
79 
80  $instance = new Administration($config, $container, $ui, fn() => $confirmation);
81  $this->assertSame('rendered', $instance->deleteDocumentsConfirmation('link', 'submitCommand', 'cancelCommand', $documents));
82  }
83 
84  public function testDeleteDocuments(): void
85  {
86  $container = $this->getMockBuilder(Container::class)->disableOriginalConstructor()->getMock();
87  $ui = $this->getMockBuilder(UI::class)->disableOriginalConstructor()->getMock();
88 
89  $documents = [
90  $this->getMockBuilder(Document::class)->disableOriginalConstructor()->getMock(),
91  $this->getMockBuilder(Document::class)->disableOriginalConstructor()->getMock(),
92  $this->getMockBuilder(Document::class)->disableOriginalConstructor()->getMock(),
93  ];
94 
95  $repository = $this->getMockBuilder(DocumentRepository::class)->getMock();
96 
97  $consecutive = $documents;
98  $repository->expects(self::exactly(count($documents)))->method('deleteDocument')->with(
99  $this->callback(function ($value) use (&$consecutive) {
100  $this->assertSame(array_shift($consecutive), $value);
101  return true;
102  })
103  );
104 
105  $config = $this->mockMethod(Config::class, 'legalDocuments', [], $this->mockMethod(
106  Provide::class,
107  'document',
108  [],
109  $this->mockMethod(ProvideDocument::class, 'repository', [], $repository)
110  ));
111 
112  $instance = new Administration($config, $container, $ui);
113  $instance->deleteDocuments($documents);
114  }
115 
116  public function testWithDocumentAndCriterion(): void
117  {
118  $container = $this->getMockBuilder(Container::class)->disableOriginalConstructor()->getMock();
119  $ui = $this->getMockBuilder(UI::class)->disableOriginalConstructor()->getMock();
120 
121  $criterion = $this->mockMethod(Criterion::class, 'id', [], 98);
122  $document = $this->mockMethod(Document::class, 'criteria', [], [
123  $this->mockMethod(Criterion::class, 'id', [], 8),
124  $criterion,
125  ]);
126 
127  $repository = $this->mockMethod(DocumentRepository::class, 'find', [459], new Ok($document));
128  $config = $this->mockMethod(Config::class, 'legalDocuments', [], $this->mockMethod(
129  Provide::class,
130  'document',
131  [],
132  $this->mockMethod(ProvideDocument::class, 'repository', [], $repository)
133  ));
134 
135  $query_params = ['legal_document_id' => ['459'], 'criterion_id' => '98'];
136 
137  $query = $this->mock(ArrayBasedRequestWrapper::class);
138 
139  $query
140  ->expects($this->exactly(2))
141  ->method('retrieve')
142  ->willReturnCallback(function ($parameter1) {
143  return match ([$parameter1]) {
144  ['legal_document_id'] => [459],
145  ['doc_id'] => null,
146  };
147  });
148 
149  $httpWrapper = $this->mockMethod(WrapperFactory::class, 'query', [], $query, $this->exactly(2));
150 
151  $http = $this->mockMethod(HTTPServices::class, 'request', [], $this->mockMethod(
152  ServerRequestInterface::class,
153  'getQueryParams',
154  [],
155  $query_params,
156  self::exactly(1)
157  ), self::exactly(1));
158  $http->method('wrapper')->willReturn($httpWrapper);
159 
160  $refinery = $this->mockMethod(Refinery::class, 'kindlyTo', [], $this->mockMethod(KindlyToGroup::class, 'int', [], $this->mockMethod(
161  Transformation::class,
162  'applyTo',
163  [new Ok('459')],
164  new Ok(459)
165  ), $this->exactly(3)), $this->exactly(4));
166 
167  $container->method('http')->willReturn($http);
168  $container->method('refinery')->willReturn($refinery);
169 
170  $called = false;
171  $instance = new Administration($config, $container, $ui);
172  $instance->withDocumentAndCriterion(function ($d, $c) use ($document, $criterion, &$called) {
173  $this->assertSame($document, $d);
174  $this->assertSame($criterion, $c);
175  $called = true;
176  });
177  $this->assertTrue($called);
178  }
179 
180  private function doc(int $id, string $title): Document
181  {
182  $document = $this->getMockBuilder(Document::class)->disableOriginalConstructor()->getMock();
183  $document->expects(self::once())->method('id')->willReturn($id);
184  $document->expects(self::once())->method('content')->willReturn(
185  $this->mockMethod(DocumentContent::class, 'title', [], $title)
186  );
187 
188  return $document;
189  }
190 }
Transformations in this group transform data to primitive types to establish a baseline for more comp...
Definition: Group.php:47
$http
Definition: deliver.php:30
$c
Definition: deliver.php:25
$container
Definition: wac.php:36
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Ok.php:30
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23