ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
RelationsTableTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
26 use ilLanguage;
27 use ilUIService;
29 use ilBuddyList;
36 use stdClass;
37 use ilDBInterface;
38 use ilCtrlInterface;
42 use Closure;
45 use ArrayIterator;
47 
48 require_once __DIR__ . '/../../../../LegalDocuments/tests/ContainerMock.php';
49 
51 {
52  use ContainerMock;
53 
54  public function testConstruct(): void
55  {
56  $this->assertInstanceOf(RelationsTable::class, new RelationsTable(
57  ...array_map($this->mock(...), [UIFactory::class, ilLanguage::class, ilUIService::class, Http::class])
58  ));
59  }
60 
61  public function testData(): void
62  {
63  if (!defined('ANONYMOUS_USER_ID')) {
64  define('ANONYMOUS_USER_ID', 7);
65  }
66  $container = [
67  'ilDB' => $this->mockTree(ilDBInterface::class, ['in' => 'dummy', 'queryF' => []]),
68  'lng' => $this->mock(ilLanguage::class),
69  'ilCtrl' => $this->mock(ilCtrlInterface::class),
70  ];
71  $original = $GLOBALS['DIC'] ?? null;
72  $GLOBALS['DIC'] = $this->mockTree(Container::class, ['user' => ['getId' => 12345]]);
73  $GLOBALS['DIC']->method('offsetGet')->willReturnCallback(fn($k) => $container[$k]);
74  $std_class = new stdClass();
75  $std_class->usr_id = 12345;
76  $std_class->public_profile = false;
77  $std_class->login = 'dummy';
78  $array = [null, $std_class, null, $std_class];
79  $container['ilDB']->expects(self::exactly(4))
80  ->method('fetchObject')
81  ->willReturnCallback(function () use (&$array) {
82  return next($array) ?: null;
83  });
84  $relation = $this->mockTree(ilBuddySystemRelation::class, ['getState' => []]);
85  $relations = $this->mockTree(ilBuddySystemRelationCollection::class, ['toArray' => [12345 => $relation]]);
86  $relations->expects(self::once())->method('filter')->willReturnCallback(function ($f) use ($relations, $relation) {
87  $this->assertTrue($f($relation));
88  return $relations;
89  });
90  $mock = $this->mockTree(ilBuddyList::class, ['getRelations' => $relations]);
91  $mapper = $this->mockMethod(ilBuddySystemRelationStateTableFilterMapper::class, 'filterMatchesRelation', ['huhu'], true);
92  $mapper->expects(self::once())->method('text')->willReturn('foo');
93  $factory = $this->mock(ilBuddySystemRelationStateFactory::class);
94  $factory->expects(self::exactly(2))->method('getTableFilterStateMapper')->with($relation->getState())->willReturn($mapper);
95 
96  $this->setBuddyList(12345, $mock);
97  $this->setStateFactory($factory);
98 
99  $this->assertEquals([[
100  'user_id' => 12345,
101  'public_name' => '',
102  'login' => 'dummy',
103  'state' => $relation->getState(),
104  'points' => [],
105  'state-text' => 'foo',
106  ]], RelationsTable::data(['state' => 'huhu']));
107 
108  $this->setStateFactory(null);
109  $this->setBuddyList(12345, null);
110  $GLOBALS['DIC'] = $original;
111  }
112 
113  public function testBuild(): void
114  {
115  $original = $GLOBALS['DIC'] ?? null;
116  $GLOBALS['DIC'] = $this->mockTree(Container::class, ['user' => []]);
117  $request = $this->mock(ServerRequestInterface::class);
118  $filter = $this->mock(Filter::class);
119  $single_action = $this->mock(Action::class);
120  $multi_actions = ['a' => $this->mock(Action::class), 'b' => $this->mock(Action::class)];
121  $actions = array_merge($multi_actions, ['->' => $single_action]);
122  $data_table = $this->mock(Table::class);
123  $data_table->expects(self::once())->method('withRequest')->with($request)->willReturn($data_table);
124  $data_table->expects(self::once())->method('withActions')->with($actions)->willReturn($data_table);
125 
126  $table = new class (
127  $this->mockTree(UIFactory::class, ['table' => ['data' => $data_table]]),
128  $this->mock(ilLanguage::class),
129  $this->mockTree(ilUIService::class, ['filter' => ['standard' => $filter]]),
130  $this->mockTree(Http::class, ['request' => $request])
131  ) extends RelationsTable {
132  public static Closure $data;
133  public static function data(array $filter = []): array
134  {
135  return (self::$data)($filter);
136  }
137  };
138  $table::$data = fn() => [
139  [
140  'user_id' => 12345,
141  'state' => $this->mock(State::class),
142  'points' => [$this->mock(State::class), $this->mock(State::class)],
143  ],
144  ];
145 
146  $this->setStateFactory($this->mock(ilBuddySystemRelationStateFactory::class));
147 
148  $components = $table->build($multi_actions, '', fn() => $single_action);
149  $this->assertSame(2, count($components));
150  $this->assertSame($filter, $components[0]);
151  $this->assertSame($data_table, $components[1]);
152 
153  $this->setStateFactory(null);
154  $GLOBALS['DIC'] = $original;
155  }
156 
157  private function setBuddyList(int $id, ?ilBuddyList $mock): void
158  {
159  $p = new ReflectionProperty(ilBuddyList::class, 'instances');
160  $array = $p->getValue();
161  $array[$id] = $mock;
162  $p->setValue(null, $array);
163  }
164 
165  private function setStateFactory(?ilBuddySystemRelationStateFactory $mock): void
166  {
167  $p = new ReflectionProperty(ilBuddySystemRelationStateFactory::class, 'instance');
168  $p->setValue(null, $mock);
169  }
170 }
$relation
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$components
$container
Definition: wac.php:36
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
$GLOBALS["DIC"]
Definition: wac.php:53
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23