ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ilObjectTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use PHPUnit\Framework\TestCase;
23
24class ilObjectTest extends TestCase
25{
31
32 protected function setUp(): void
33 {
34 global $DIC;
35 $this->dic_backup = is_object($DIC) ? clone $DIC : $DIC;
36
37 $DIC = new Container();
38 $DIC['ilias'] = $this->createMock(ILIAS::class);
39 $DIC['objDefinition'] = $this->createMock(ilObjectDefinition::class);
40 $DIC['ilDB'] = $this->db_mock = $this->createMock(ilDBInterface::class);
41 $DIC['ilLog'] = $this->createMock(ilLogger::class);
42 $DIC['ilErr'] = $this->createMock(ilErrorHandling::class);
43 $DIC['tree'] = $this->createMock(ilTree::class);
44 $DIC['ilAppEventHandler'] = $this->createMock(ilAppEventHandler::class);
45 $DIC['ilUser'] = $this->createMock(ilObjUser::class);
46 $DIC['resource_storage'] = $this->createMock(ILIAS\ResourceStorage\Services::class);
47 $DIC['object.customicons.factory'] = $this->createMock(ILIAS\ILIASObject\Properties\AdditionalProperties\Icon\Factory::class);
48 $DIC['learning_object_metadata'] = $this->createMock(ILIAS\MetaData\Services\ServicesInterface::class);
49 $DIC['user'] = $this->createMock(ILIAS\User\PublicInterface::class);
50
51 if (!defined('ILIAS_LOG_DIR')) {
52 define('ILIAS_LOG_DIR', '/var/log');
53 }
54
55 if (!defined('ILIAS_LOG_FILE')) {
56 define('ILIAS_LOG_FILE', '/var/log/ilias.log');
57 }
58
59 if (!defined('ILIAS_LOG_ENABLED')) {
60 define('ILIAS_LOG_ENABLED', true);
61 }
62 }
63
64 protected function tearDown(): void
65 {
66 global $DIC;
68 }
69
70 public function testCreationDeletion(): void
71 {
72 $obj = new ilObject();
73 $obj->setType("xxx");
74
75 $this->db_mock->expects($this->any())
76 ->method('nextId')
78 ->willReturnOnConsecutiveCalls(21, 22, 23);
79
80 $str = '2022-04-28 08:00:00';
81 $this->db_mock->expects($this->any())
82 ->method('fetchAssoc')
83 ->willReturnOnConsecutiveCalls(
84 ['last_update' => $str, 'create_date' => $str],
85 ['last_update' => $str, 'create_date' => $str],
86 ['last_update' => $str, 'create_date' => $str]
87 );
88
89 $obj->create();
90 $id = $obj->getId();
91 $this->assertEquals(21, $id);
92
93 $obj->create();
94 $id = $obj->getId();
95 $this->assertEquals(22, $id);
96
97 $obj->create();
98 $id = $obj->getId();
99 $this->assertEquals(23, $id);
100 }
101
102 /*
103
104 alex, 25.1.2023
105
106 I outcommented this test, since it fails "sometimes".
107 The way fetchAssoc is mocked seems to run into issues, if unexpected tables are queried
108 during the object instantiation.
109
110 e.g.:
111 ilObjectTest::testSetGetLookup
112 Undefined array key "keyword"
113
114 /home/runner/work/ILIAS/ILIAS/Services/Administration/classes/class.ilSetting.php:92
115 /home/runner/work/ILIAS/ILIAS/Services/Administration/classes/class.ilSetting.php:62
116 /home/runner/work/ILIAS/ILIAS/Services/Logging/classes/class.ilLoggingDBSettings.php:37
117 /home/runner/work/ILIAS/ILIAS/Services/Logging/classes/class.ilLoggingDBSettings.php:46
118 /home/runner/work/ILIAS/ILIAS/Services/Logging/classes/public/class.ilLoggerFactory.php:69
119 /home/runner/work/ILIAS/ILIAS/Services/Logging/classes/public/class.ilLoggerFactory.php:91
120 /home/runner/work/ILIAS/ILIAS/Services/Object/classes/class.ilObject.php:90
121 /home/runner/work/ILIAS/ILIAS/Services/Object/tests/ilObjectTest.php:106
122
123 public function testSetGetLookup(): void
124 {
125 global $DIC;
126 $ilUser = $DIC->user();
127
128 $this->db_mock->expects($this->any())
129 ->method('nextId')
130 ->withConsecutive([ilObject::TABLE_OBJECT_DATA], ['object_reference'])
131 ->willReturnOnConsecutiveCalls(21, 22);
132
133 $str = '2022-04-28 08:00:00';
134 $this->db_mock->expects($this->any())
135 ->method('fetchAssoc')
136 ->willReturnOnConsecutiveCalls(
137 ['last_update' => $str, 'create_date' => $str],
138 ['last_update' => $str, 'create_date' => $str],
139 ['last_update' => $str, 'create_date' => $str]
140 );
141
142
143 $obj = new ilObject();
144 $obj->setType("xxx"); // otherwise type check will fail
145 $obj->setTitle("TestObject");
146 $obj->setDescription("TestDescription");
147 $obj->setImportId("imp_44");
148 $obj->create();
149 $obj->createReference();
150 $id = $obj->getId();
151 $ref_id = $obj->getRefId();
152 $this->assertEquals(21, $id);
153 $this->assertEquals(22, $ref_id);
154
155
156 // Reading
157 $DIC['ilDB'] = $this->db_mock = $this->createMock(ilDBInterface::class);
158 $ilDBStatement = $this->createMock(ilDBStatement::class);
159 $this->db_mock->expects($this->any())
160 ->method('query')
161 ->with("SELECT obj_id, type, title, description, owner, create_date, last_update, import_id, offline" . PHP_EOL
162 . "FROM " . ilObject::TABLE_OBJECT_DATA . PHP_EOL
163 . "WHERE obj_id = " . $this->db_mock->quote(21, "integer") . PHP_EOL)
164 ->willReturn($ilDBStatement);
165
166 $this->db_mock->expects($this->once())
167 ->method('numRows')
168 ->with($ilDBStatement)
169 ->willReturn(1);
170
171 $this->db_mock->expects($this->once())
172 ->method('fetchAssoc')
173 ->with($ilDBStatement)
174 ->willReturn([
175 'obj_id' => 21,
176 'type' => 'xxx',
177 'title' => 'TestObject',
178 'description' => 'TestDescription',
179 'owner' => 6,
180 'create_date' => '',
181 'last_update' => '',
182 'import_id' => 'imp_44',
183 'offline' => false,
184 ]);
185
186 $obj = new ilObject($id, false);
187
188 $this->assertEquals(21, $obj->getId());
189 $this->assertEquals('TestObject', $obj->getTitle());
190 $this->assertEquals('TestDescription', $obj->getDescription());
191 $this->assertEquals('imp_44', $obj->getImportId());
192 $this->assertEquals(6, $obj->getOwner());
193 }
194 */
195}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:36
ilDBInterface $db_mock
ILIAS DI Container $dic_backup
Class ilObject Basic functions for all objects.
const TABLE_OBJECT_DATA
Interface ilDBInterface.
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
global $DIC
Definition: shib_login.php:26