ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
AccessFileUploadPreviewTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\Test\test;
22 
26 use ilDBInterface;
27 use ilDBStatement;
28 use ilDBConstants;
29 use ilAccessHandler;
30 use Closure;
31 
32 class AccessFileUploadPreviewTest extends TestCase
33 {
34  public function testConstruct(): void
35  {
36  $database = $this->getMockBuilder(ilDBInterface::class)->disableOriginalConstructor()->getMock();
37  $access = $this->getMockBuilder(ilAccessHandler::class)->getMock();
38  $this->assertInstanceOf(AccessFileUploadPreview::class, new AccessFileUploadPreview($database, $access));
39  }
40 
41  public function testNoUploadPath(): void
42  {
43  $database = $this->getMockBuilder(ilDBInterface::class)->disableOriginalConstructor()->getMock();
44  $access = $this->getMockBuilder(ilAccessHandler::class)->getMock();
45 
46  $instance = new AccessFileUploadPreview($database, $access);
47  $result = $instance->isPermitted('/data/some/path/file.pdf');
48  $this->assertFalse($result->isOk());
49  }
50 
51  public function testFalseWithInvalidId(): void
52  {
53  $database = $this->getMockBuilder(ilDBInterface::class)->disableOriginalConstructor()->getMock();
54  $access = $this->getMockBuilder(ilAccessHandler::class)->getMock();
55  $statement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
56 
57  $database->expects(self::once())->method('queryF')->with('SELECT obj_fi FROM qpl_questions WHERE question_id = %s', [ilDBConstants::T_INTEGER], [383])->willReturn($statement);
58  $database->expects(self::once())->method('fetchAssoc')->with($statement)->willReturn(null);
59 
60  $instance = new AccessFileUploadPreview($database, $access);
61  $result = $instance->isPermitted('http://my-ilias/assessment/qst_preview/123/383/fileuploads/my-file.pdf');
62  $this->assertTrue($result->isOk());
63  $this->assertFalse($result->value());
64  }
65 
66  #[\PHPUnit\Framework\Attributes\DataProvider('types')]
67  public function testWithTypes(?string $type, bool $permitted, ?string $requires_permission): void
68  {
69  $database = $this->getMockBuilder(ilDBInterface::class)->disableOriginalConstructor()->getMock();
70  $access = $this->getMockBuilder(ilAccessHandler::class)->getMock();
71  $statement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
72  $incident = $this->getMockBuilder(Incident::class)->disableOriginalConstructor()->getMock();
73 
74  $ref_called = 0;
75  $type_called = 0;
76  $references_of = $this->expectCall(383, [987], $ref_called);
77  $type_of = $this->expectCall(987, $type, $type_called);
78 
79  $database->expects(self::once())->method('queryF')->with('SELECT obj_fi FROM qpl_questions WHERE question_id = %s', [ilDBConstants::T_INTEGER], [383])->willReturn($statement);
80  $database->expects(self::once())->method('fetchAssoc')->with($statement)->willReturn(['obj_fi' => '383']);
81 
82  $incident->expects(self::once())->method('any')->willReturnCallback(function (callable $call_me, array $ref_ids): bool {
83  $this->assertEquals([987], $ref_ids);
84  return $call_me(987);
85  });
86 
87  if (null === $requires_permission) {
88  $access->expects(self::never())->method('checkAccess');
89  } else {
90  $access->expects(self::once())->method('checkAccess')->with($requires_permission, '', 987)->willReturn($permitted);
91  }
92 
93 
94  $instance = new AccessFileUploadPreview($database, $access, $incident, $references_of, $type_of);
95  $result = $instance->isPermitted('http://my-ilias/assessment/qst_preview/123/383/fileuploads/my-file.pdf');
96  $this->assertTrue($result->isOk());
97  $this->assertSame($permitted, $result->value());
98 
99  $this->assertSame(1, $ref_called);
100  $this->assertSame(1, $type_called);
101  }
102 
103  public static function types(): array
104  {
105  return [
106  'Type qpl with access rights.' => ['qpl', false, 'read'],
107  'Type qpl without access rights.' => ['qpl', true, 'read'],
108  'Type tst with access rights.' => ['tst', false, 'write'],
109  'Type tst without access rights.' => ['tst', true, 'write'],
110  'Type crs will never has access rights.' => ['crs', false, null],
111  'Unknown types will never have access rights.' => [null, false, null],
112  ];
113  }
114 
115  private function expectCall($expected, $return, &$called): Closure
116  {
117  return function ($value) use ($expected, $return, &$called) {
118  $this->assertSame($expected, $value);
119  $called++;
120  return $return;
121  };
122  }
123 }
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
testWithTypes(?string $type, bool $permitted, ?string $requires_permission)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...