ILIAS  trunk Revision v12.0_alpha-1221-g4e438232683
ilModulesFileTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use PHPUnit\Framework\MockObject\MockObject;
23use PHPUnit\Framework\Attributes\PreserveGlobalState;
24use PHPUnit\Framework\Attributes\RunInSeparateProcess;
25use PHPUnit\Framework\TestCase;
35
36class ilModulesFileTest extends TestCase
37{
38 private ?Container $dic_backup = null;
42 protected ?MockObject $storage_mock = null;
46 protected ?MockObject $db_mock = null;
47 protected MockObject $manager_mock;
48
49 protected function setUp(): void
50 {
51 global $DIC;
52 $this->dic_backup = is_object($DIC) ? clone $DIC : null;
53
54 $DIC = new Container();
55 $DIC['resource_storage'] = $this->storage_mock = $this->createMock(Services::class);
56 $this->manager_mock = $this->createMock(Manager::class);
57 $DIC['ilUser'] = $this->createMock(ilObjUser::class);
58 $DIC['ilUser']->method('getPref')->willReturn('en');
59 $DIC['ilDB'] = $this->db_mock = $this->createMock(ilDBInterface::class);
60 $DIC['upload'] = $this->createMock(FileUpload::class);
61 $DIC['ilias'] = $this->createMock(ILIAS::class);
62 $DIC['objDefinition'] = $this->createMock(ilObjectDefinition::class);
63 $DIC['ilLog'] = $this->createMock(ilLogger::class);
64 $DIC['ilErr'] = $this->createMock(ilErrorHandling::class);
65 $DIC['tree'] = $this->createMock(ilTree::class);
66 $DIC['tpl'] = $this->createMock(ilGlobalTemplateInterface::class);
67 $DIC['ilClientIniFile'] = $this->createMock(ilIniFile::class);
68 $DIC['ilAccess'] = $this->createMock(ilAccess::class);
69 $DIC['ilObjDataCache'] = $this->createMock(ilObjectDataCache::class);
70 $DIC['ilAppEventHandler'] = $this->createMock(ilAppEventHandler::class);
71 $DIC['lng'] = $this->createMock(ilLanguage::class);
72 $DIC['ilCtrl'] = $this->createMock(ilCtrlInterface::class);
73 $DIC['refinery'] = $this->createMock(Factory::class);
74 $DIC['http'] = $this->createMock(\ILIAS\HTTP\Services::class);
75 $DIC['object.customicons.factory'] = $this->createMock(IconFactory::class);
76 /* $DIC['ilCtrl'] = $this->getMockBuilder(ilCtrl::class)
77 ->disableOriginalConstructor()
78 ->disableArgumentCloning()
79 ->getMock();*/
80
81 if (!defined('ILIAS_LOG_ENABLED')) {
82 define('ILIAS_LOG_ENABLED', false);
83 }
84 }
85
86 protected function tearDown(): void
87 {
88 global $DIC;
90 }
91
92 #[PreserveGlobalState(false)]
93 #[RunInSeparateProcess]
94 public function testAppendStream(): void
95 {
96 $this->markTestSkipped('Failed for some unknown reason.');
97
98 // DB mock
99 $title = 'Revision One';
100 $file_stream = Streams::ofString('Test Content');
101
102 $this->storage_mock
103 ->method('manage')
104 ->willReturn($this->manager_mock);
105
106 $this->db_mock
107 ->method('query')
108 ->willReturnCallback(function ($query): MockObject {
109 $mock_object = $this->createMock(ilDBStatement::class);
110 $mock_object->method('fetchAssoc')->willReturn([$query]);
111
112 return $mock_object;
113 });
114
115 $this->db_mock
116 ->method('fetchAssoc')
117 ->willReturnCallback(function (ilDBStatement $statement): ?array {
118 $row = $statement->fetchAssoc();
119 $query = '';
120 if ($row !== null) {
121 $query = end($row);
122 }
123 if (str_contains($query, 'last_update')) {
124 return [
125 'last_update' => '',
126 'create_date' => ''
127 ];
128 }
129
130 return null;
131 });
132
133 // Create File Object with disabled news notification
134 $file = $this->getMockBuilder(ilObjFile::class)
135 ->onlyMethods(['update'])
136 ->getMock();
137 $file->method('update');
138
139 $r = new ReflectionClass(ilObjFile::class);
140 $property = $r->getProperty('just_notified');
141 $property->setValue($file, true);
142 $file->setMode(ilObjFile::MODE_FILELIST);
143 $this->db_mock
144 ->method('fetchAssoc')
145 ->willReturn(
146 [
147 'last_update' => '',
148 'create_date' => ''
149 ]
150 );
151 $file->create();
152
153 // identification
154 $rid = new ResourceIdentification('the_identification');
155
156 $consecutive = [
157 ['-', null],
158 ['the_identification', $rid],
159 ['the_identification', $rid],
160 ];
161 $this->manager_mock
162 ->method('find')
163 ->willReturnCallback(
164 function (string $id) use (&$consecutive): ?ResourceIdentification {
165 $expected = array_shift($consecutive);
166 [$eid, $ret] = $consecutive;
167 $this->assertEquals($eid, $id);
168 return $ret;
169 }
170 );
171
172 $this->manager_mock->expects($this->once())
173 ->method('stream')
174 ->with($file_stream, new ilObjFileStakeholder(0), $title)
175 ->willReturn($rid);
176
177 $revision = new FileRevision($rid);
178 $revision->setVersionNumber(1);
179 $revision->setTitle($title);
180 $resource = new StorableFileResource($rid);
181 $resource->addRevision($revision);
182
183 $this->manager_mock->expects($this->once())
184 ->method('getCurrentRevision')
185 ->with($rid)
186 ->willReturn($revision);
187
188 $this->manager_mock
189 ->method('getResource')
190 ->with($rid)
191 ->willReturn($resource);
192
193 $revision_number = $file->appendStream($file_stream, $title);
194 $this->assertEquals(1, $revision_number);
195 $this->assertEquals(1, $file->getVersion());
196 $this->assertEquals($title, $file->getTitle());
197 }
198}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:36
Builds data types.
Definition: Factory.php:36
Stream factory which enables the user to create streams without the knowledge of the concrete class.
Definition: Streams.php:32
Class ilObjFileStakeholder.
const MODE_FILELIST
Interface ilDBStatement.
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
global $DIC
Definition: shib_login.php:26