]> git.agnieray.net Git - galette.git/blob - tests/Galette/Entity/tests/units/Document.php
b0ae7cb287f002b54909b972a8a32f799732a1ca
[galette.git] / tests / Galette / Entity / tests / units / Document.php
1 <?php
2
3 /**
4 * Copyright © 2003-2024 The Galette Team
5 *
6 * This file is part of Galette (https://galette.eu).
7 *
8 * Galette is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * Galette is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 namespace Galette\Entity\test\units;
23
24 use Galette\GaletteTestCase;
25
26 /**
27 * Status tests
28 *
29 * @author Johan Cwiklinski <johan@x-tnd.be>
30 */
31 class Document extends GaletteTestCase
32 {
33 protected int $seed = 20240312213127;
34
35 /**
36 * Tear down tests
37 *
38 * @return void
39 */
40 public function tearDown(): void
41 {
42 parent::tearDown();
43
44 $this->deleteDocuments();
45
46 //drop dynamic translations
47 $delete = $this->zdb->delete(\Galette\Core\L10n::TABLE);
48 $this->zdb->execute($delete);
49 }
50
51 /**
52 * Delete documents
53 *
54 * @return void
55 */
56 private function deleteDocuments(): void
57 {
58 $delete = $this->zdb->delete(\Galette\Entity\Document::TABLE);
59 $this->zdb->execute($delete);
60 }
61
62 /**
63 * Test document object
64 *
65 * @return void
66 */
67 public function testObject(): void
68 {
69 $document = new \Galette\Entity\Document($this->zdb);
70
71 //getters only
72 $this->assertSame('', $document->getDocumentFilename());
73 $this->assertSame($document->getDestDir(), $document->getURL());
74 $this->assertNull($document->getID());
75
76 //setters and getters
77 $this->assertSame('', $document->getType());
78 $this->assertInstanceOf(\Galette\Entity\Document::class, $document->setType('mytype'));
79 $this->assertSame('mytype', $document->getType());
80
81 $this->assertNull($document->getComment());
82 $this->assertInstanceOf(\Galette\Entity\Document::class, $document->setComment('any comment'));
83 $this->assertSame('any comment', $document->getComment());
84 }
85
86 /**
87 * Test document "system" types
88 *
89 * @return void
90 */
91 public function testGetSystemTypes(): void
92 {
93 $document = new \Galette\Entity\Document($this->zdb);
94 $this->assertCount(5, $document->getSystemTypes());
95 }
96
97 //FIXME: not possible to test real document, since all relies on a file upload...
98
99 /**
100 * Get mocked document instance
101 *
102 * @return \Galette\Entity\Document
103 */
104 private function getDocumentInstance(): \Galette\Entity\Document
105 {
106 $document = $this->getMockBuilder(\Galette\Entity\Document::class)
107 ->setConstructorArgs(array($this->zdb))
108 ->onlyMethods(array('handleFiles'))
109 ->getMock();
110
111 $document->method('handleFiles')
112 ->willReturnCallback(
113 function (array $files) use ($document) {
114 $reflection = new \ReflectionClass(\Galette\Entity\Document::class);
115 $reflection_property = $reflection->getProperty('filename');
116 $reflection_property->setAccessible(true);
117 $reflection_property->setValue($document, $files['document_file']['name']);
118
119 return true;
120 }
121 );
122 return $document;
123 }
124
125 /**
126 * Test getList
127 *
128 * @return void
129 */
130 public function testGetList(): void
131 {
132 $document = $this->getDocumentInstance();
133
134 // no document yet, list is empty
135 $this->assertSame([], $document->getList());
136
137 $_FILES['document_file'] = [
138 'error' => UPLOAD_ERR_OK,
139 'name' => 'status.pdf',
140 'tmp_name' => '/tmp/status.pdf',
141 'size' => 2048
142 ];
143 $post = [
144 'document_type' => \Galette\Entity\Document::STATUS,
145 'comment' => 'Status of the association',
146 'visible' => \Galette\Entity\FieldsConfig::ALL
147 ];
148
149 $this->assertTrue($document->store($post, $_FILES));
150
151 //test list
152 $list = $document->getList();
153 $this->assertCount(1, $list);
154
155 $entry = array_pop($list);
156 $this->assertSame('status.pdf', $entry->getDocumentFilename());
157 $this->assertSame(\Galette\Entity\Document::STATUS, $entry->getType());
158 $this->assertSame('Status of the association', $entry->getComment());
159 $this->assertSame(\Galette\Entity\FieldsConfig::ALL, $entry->getPermission());
160 $this->assertSame('Public', $entry->getPermissionName());
161
162 //test list by type (for public pages)
163 $tlist = $document->getTypedList();
164 $this->assertCount(1, $tlist);
165 $this->assertArrayHasKey(\Galette\Entity\Document::STATUS, $tlist);
166 $this->assertCount(1, $tlist[\Galette\Entity\Document::STATUS]);
167
168 //"upload" another document
169 $document = $this->getDocumentInstance();
170 $_FILES['document_file'] = [
171 'error' => UPLOAD_ERR_OK,
172 'name' => 'afile.pdf',
173 'tmp_name' => '/tmp/afile.pdf',
174 'size' => 4096
175 ];
176 $post = [
177 'document_type' => 'An other document type',
178 'comment' => '',
179 'visible' => \Galette\Entity\FieldsConfig::ADMIN
180 ];
181
182 $this->assertTrue($document->store($post, $_FILES));
183
184 //test list - not authenticated
185 $list = $document->getList();
186 $this->assertCount(1, $list);
187
188 //test list - authenticated
189 $this->logSuperAdmin();
190 $list = $document->getList();
191 $this->assertCount(2, $list);
192
193 //test list by type (for public pages)
194 $tlist = $document->getTypedList();
195 $this->assertCount(2, $tlist);
196 $this->assertArrayHasKey(\Galette\Entity\Document::STATUS, $tlist);
197 $this->assertArrayHasKey('An other document type', $tlist);
198 $this->assertCount(1, $tlist[\Galette\Entity\Document::STATUS]);
199 $this->assertCount(1, $tlist['An other document type']);
200 }
201 }