4 * Copyright © 2003-2024 The Galette Team
6 * This file is part of Galette (https://galette.eu).
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.
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.
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/>.
22 namespace Galette\Entity\test\units
;
24 use Galette\GaletteTestCase
;
29 * @author Johan Cwiklinski <johan@x-tnd.be>
31 class Document
extends GaletteTestCase
33 protected int $seed = 20240312213127;
40 public function tearDown(): void
44 $this->deleteDocuments();
46 //drop dynamic translations
47 $delete = $this->zdb
->delete(\Galette\Core\L10n
::TABLE
);
48 $this->zdb
->execute($delete);
56 private function deleteDocuments(): void
58 $delete = $this->zdb
->delete(\Galette\Entity\Document
::TABLE
);
59 $this->zdb
->execute($delete);
63 * Test document object
67 public function testObject(): void
69 $document = new \Galette\Entity\
Document($this->zdb
);
72 $this->assertSame('', $document->getDocumentFilename());
73 $this->assertSame($document->getDestDir(), $document->getURL());
74 $this->assertNull($document->getID());
77 $this->assertSame('', $document->getType());
78 $this->assertInstanceOf(\Galette\Entity\Document
::class, $document->setType('mytype'));
79 $this->assertSame('mytype', $document->getType());
81 $this->assertNull($document->getComment());
82 $this->assertInstanceOf(\Galette\Entity\Document
::class, $document->setComment('any comment'));
83 $this->assertSame('any comment', $document->getComment());
87 * Test document "system" types
91 public function testGetSystemTypes(): void
93 $document = new \Galette\Entity\
Document($this->zdb
);
94 $this->assertCount(5, $document->getSystemTypes());
97 //FIXME: not possible to test real document, since all relies on a file upload...
100 * Get mocked document instance
102 * @return \Galette\Entity\Document
104 private function getDocumentInstance(): \Galette\Entity\Document
106 $document = $this->getMockBuilder(\Galette\Entity\Document
::class)
107 ->setConstructorArgs(array($this->zdb
))
108 ->onlyMethods(array('handleFiles'))
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']);
130 public function testGetList(): void
132 $document = $this->getDocumentInstance();
134 // no document yet, list is empty
135 $this->assertSame([], $document->getList());
137 $_FILES['document_file'] = [
138 'error' => UPLOAD_ERR_OK
,
139 'name' => 'status.pdf',
140 'tmp_name' => '/tmp/status.pdf',
144 'document_type' => \Galette\Entity\Document
::STATUS
,
145 'comment' => 'Status of the association',
146 'visible' => \Galette\Entity\FieldsConfig
::ALL
149 $this->assertTrue($document->store($post, $_FILES));
152 $list = $document->getList();
153 $this->assertCount(1, $list);
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());
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
]);
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',
177 'document_type' => 'An other document type',
179 'visible' => \Galette\Entity\FieldsConfig
::ADMIN
182 $this->assertTrue($document->store($post, $_FILES));
184 //test list - not authenticated
185 $list = $document->getList();
186 $this->assertCount(1, $list);
188 //test list - authenticated
189 $this->logSuperAdmin();
190 $list = $document->getList();
191 $this->assertCount(2, $list);
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']);