ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
AccessFileUploadPreviewTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\Modules\Test\test;
22 
26 use ilDBInterface;
27 use ilDBStatement;
28 use ilDBConstants;
29 use ilAccess;
30 use Closure;
31 
33 {
34  public function testConstruct() : void
35  {
36  $database = $this->getMockBuilder(ilDBInterface::class)->disableOriginalConstructor()->getMock();
37  $access = $this->getMockBuilder(ilAccess::class)->disableOriginalConstructor()->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(ilAccess::class)->disableOriginalConstructor()->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(ilAccess::class)->disableOriginalConstructor()->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 
69  public function testWithTypes(?string $type, bool $permitted, ?string $requires_permission) : void
70  {
71  $database = $this->getMockBuilder(ilDBInterface::class)->disableOriginalConstructor()->getMock();
72  $access = $this->getMockBuilder(ilAccess::class)->disableOriginalConstructor()->getMock();
73  $statement = $this->getMockBuilder(ilDBStatement::class)->disableOriginalConstructor()->getMock();
74  $incident = $this->getMockBuilder(Incident::class)->disableOriginalConstructor()->getMock();
75 
76  $ref_called = 0;
77  $type_called = 0;
78  $references_of = $this->expectCall(383, ['987'], $ref_called);
79  $type_of = $this->expectCall(987, $type, $type_called);
80 
81  $database->expects(self::once())->method('queryF')->with('SELECT obj_fi FROM qpl_questions WHERE question_id = %s', [ilDBConstants::T_INTEGER], [383])->willReturn($statement);
82  $database->expects(self::once())->method('fetchAssoc')->with($statement)->willReturn(['obj_fi' => '383']);
83 
84  $incident->expects(self::once())->method('any')->willReturnCallback(function (callable $call_me, array $ref_ids) : bool {
85  $this->assertEquals(['987'], $ref_ids);
86  return $call_me('987');
87  });
88 
89  if (null === $requires_permission) {
90  $access->expects(self::never())->method('checkAccess');
91  } else {
92  $access->expects(self::once())->method('checkAccess')->with($requires_permission, '', 987)->willReturn($permitted);
93  }
94 
95 
96  $instance = new AccessFileUploadPreview($database, $access, $incident, $references_of, $type_of);
97  $result = $instance->isPermitted('http://my-ilias/assessment/qst_preview/123/383/fileuploads/my-file.pdf');
98  $this->assertTrue($result->isOk());
99  $this->assertSame($permitted, $result->value());
100 
101  $this->assertSame(1, $ref_called);
102  $this->assertSame(1, $type_called);
103  }
104 
105  public function types() : array
106  {
107  return [
108  'Type qpl with access rights.' => ['qpl', false, 'read'],
109  'Type qpl without access rights.' => ['qpl', true, 'read'],
110  'Type tst with access rights.' => ['tst', false, 'write'],
111  'Type tst without access rights.' => ['tst', true, 'write'],
112  'Type crs will never has access rights.' => ['crs', false, null],
113  'Unknown types will never have access rights.' => [null, false, null],
114  ];
115  }
116 
117  private function expectCall($expected, $return, &$called): Closure
118  {
119  return function ($value) use ($expected, $return, &$called) {
120  $this->assertSame($expected, $value);
121  $called++;
122  return $return;
123  };
124  }
125 }
testWithTypes(?string $type, bool $permitted, ?string $requires_permission)
types
$result
$type