ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
ilModulesFileTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
30 
31 class ilModulesFileTest extends TestCase
32 {
37  protected $storage_mock;
41  protected $db_mock;
42  protected $manager_mock;
43 
44  protected function setUp(): void
45  {
46  global $DIC;
47  $this->dic_backup = is_object($DIC) ? clone $DIC : null;
48 
49  $DIC = new Container();
50  $DIC['resource_storage'] = $this->storage_mock = $this->createMock(Services::class);
51  $this->manager_mock = $this->createMock(Manager::class);
52  $DIC['ilUser'] = $this->createMock(ilObjUser::class);
53  $DIC['ilUser']->expects($this->any())->method('getPref')->willReturn('en');
54  $DIC['ilDB'] = $this->db_mock = $this->createMock(ilDBInterface::class);
55  $DIC['upload'] = $this->createMock(FileUpload::class);
56  $DIC['ilias'] = $this->createMock(ILIAS::class);
57  $DIC['objDefinition'] = $this->createMock(ilObjectDefinition::class);
58  $DIC['ilLog'] = $this->createMock(ilLogger::class);
59  $DIC['ilErr'] = $this->createMock(ilErrorHandling::class);
60  $DIC['tree'] = $this->createMock(ilTree::class);
61  $DIC['tpl'] = $this->createMock(ilGlobalTemplateInterface::class);
62  $DIC['ilClientIniFile'] = $this->createMock(ilIniFile::class);
63  $DIC['ilAccess'] = $this->createMock(ilAccess::class);
64  $DIC['ilObjDataCache'] = $this->createMock(ilObjectDataCache::class);
65  $DIC['ilAppEventHandler'] = $this->createMock(ilAppEventHandler::class);
66  $DIC['lng'] = $this->createMock(ilLanguage::class);
67  $DIC['ilCtrl'] = $this->createMock(ilCtrlInterface::class);
68  $DIC['refinery'] = $this->createMock(\ILIAS\Refinery\Factory::class);
69  $DIC['http'] = $this->createMock(\ILIAS\HTTP\Services::class);
70  $DIC['object.customicons.factory'] = $this->createMock(ilObjectCustomIconFactory::class);
71  /* $DIC['ilCtrl'] = $this->getMockBuilder(ilCtrl::class)
72  ->disableOriginalConstructor()
73  ->disableArgumentCloning()
74  ->getMock();*/
75 
76  if (!defined('ILIAS_LOG_ENABLED')) {
77  define('ILIAS_LOG_ENABLED', false);
78  }
79  }
80 
81  protected function tearDown(): void
82  {
83  global $DIC;
84  $DIC = $this->dic_backup;
85  }
86 
91  public function testAppendStream(): void
92  {
93  // DB mock
94  $title = 'Revision One';
95  $file_stream = Streams::ofString('Test Content');
96 
97  $this->storage_mock->expects($this->any())
98  ->method('manage')
99  ->willReturn($this->manager_mock);
100 
101  $this->db_mock->expects($this->any())
102  ->method('query')
103  ->willReturnCallback(function ($query) {
104  $mock_object = $this->createMock(ilDBStatement::class);
105  $mock_object->expects($this->any())->method('fetchAssoc')->willReturn([$query]);
106 
107  return $mock_object;
108  });
109 
110  $this->db_mock->expects($this->any())
111  ->method('fetchAssoc')
112  ->willReturnCallback(function (ilDBStatement $statement): ?array {
113  $row = $statement->fetchAssoc();
114  $query = '';
115  if ($row !== null) {
116  $query = end($row);
117  }
118  if (str_contains($query, 'last_update')) {
119  return [
120  'last_update' => '',
121  'create_date' => ''
122  ];
123  }
124 
125  return null;
126  });
127 
128  // Create File Object with disabled news notification
129  $file = $this->getMockBuilder(ilObjFile::class)
130  ->onlyMethods(['update'])
131  ->getMock();
132  $file->method('update');
133 
134  $r = new ReflectionClass(ilObjFile::class);
135  $property = $r->getProperty('just_notified');
136  $property->setAccessible(true);
137  $property->setValue($file, true);
138  $file->setMode(ilObjFile::MODE_FILELIST);
139  $this->db_mock->expects($this->any())
140  ->method('fetchAssoc')
141  ->willReturn(
142  [
143  'last_update' => '',
144  'create_date' => ''
145  ]
146  );
147  $file->create();
148 
149  // identification
150  $rid = new ResourceIdentification('the_identification');
151 
152  $this->manager_mock->expects($this->any())
153  ->method('find')
154  ->withConsecutive(['-'], ['the_identification'], ['the_identification'])
155  ->willReturnOnConsecutiveCalls(null, $rid, $rid);
156 
157  $this->manager_mock->expects($this->once())
158  ->method('stream')
159  ->with($file_stream, new ilObjFileStakeholder(0), $title)
160  ->willReturn($rid);
161 
162  $revision = new FileRevision($rid);
163  $revision->setVersionNumber(1);
164  $revision->setTitle($title);
165  $resource = new StorableFileResource($rid);
166  $resource->addRevision($revision);
167 
168  $this->manager_mock->expects($this->once())
169  ->method('getCurrentRevision')
170  ->with($rid)
171  ->willReturn($revision);
172 
173  $this->manager_mock->expects($this->any())
174  ->method('getResource')
175  ->with($rid)
176  ->willReturn($resource);
177 
178  $revision_number = $file->appendStream($file_stream, $title);
179  $this->assertEquals(1, $revision_number);
180  $this->assertEquals(1, $file->getVersion());
181  $this->assertEquals($title, $file->getTitle());
182  }
183 }
Class ilObjFileStakeholder.
testAppendStream()
disabled
ILIAS DI Container $dic_backup
Class ChatMainBarProvider .
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:35
global $DIC
Definition: feed.php:28
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const MODE_FILELIST
$r