|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2025 LibreCode coop and contributors |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + */ |
| 8 | + |
| 9 | +namespace OCA\Libresign\Tests\Unit\Service; |
| 10 | + |
| 11 | +use OCA\Libresign\Db\FileElement; |
| 12 | +use OCA\Libresign\Service\FileElementService; |
| 13 | +use OCA\Libresign\Tests\Unit\TestCase; |
| 14 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 15 | + |
| 16 | +final class FileElementServiceTest extends TestCase { |
| 17 | + private function getService(): FileElementService { |
| 18 | + $fileMapper = $this->createMock(\OCA\Libresign\Db\FileMapper::class); |
| 19 | + $fileElementMapper = $this->createMock(\OCA\Libresign\Db\FileElementMapper::class); |
| 20 | + $timeFactory = $this->createMock(\OCP\AppFramework\Utility\ITimeFactory::class); |
| 21 | + |
| 22 | + return new FileElementService($fileMapper, $fileElementMapper, $timeFactory); |
| 23 | + } |
| 24 | + |
| 25 | + #[DataProvider('dataFormatVisibleElements')] |
| 26 | + public function testFormatVisibleElements(array $visibleElements, array $expectedChecks): void { |
| 27 | + $service = $this->getService(); |
| 28 | + |
| 29 | + $fileElements = array_map(function ($data) { |
| 30 | + $element = new FileElement(); |
| 31 | + $element->setId($data['id']); |
| 32 | + $element->setSignRequestId($data['sign_request_id']); |
| 33 | + $element->setType($data['type']); |
| 34 | + $element->setFileId($data['file_id']); |
| 35 | + $element->setPage($data['page']); |
| 36 | + $element->setUrx((int)$data['urx']); |
| 37 | + $element->setUry((int)$data['ury']); |
| 38 | + $element->setLlx((int)$data['llx']); |
| 39 | + $element->setLly((int)$data['lly']); |
| 40 | + $element->setMetadata($data['metadata']); |
| 41 | + return $element; |
| 42 | + }, $visibleElements); |
| 43 | + |
| 44 | + $result = $service->formatVisibleElements($fileElements); |
| 45 | + |
| 46 | + $this->assertIsArray($result); |
| 47 | + |
| 48 | + foreach ($expectedChecks as $index => $checks) { |
| 49 | + $this->assertArrayHasKey($index, $result); |
| 50 | + $coords = $result[$index]['coordinates']; |
| 51 | + foreach ($checks as $key => $value) { |
| 52 | + $this->assertEquals($value, $coords[$key], "unexpected {$key} for element {$index}"); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public static function dataFormatVisibleElements(): array { |
| 58 | + return [ |
| 59 | + 'single with string coords' => [ |
| 60 | + [ |
| 61 | + [ |
| 62 | + 'id' => 123, |
| 63 | + 'sign_request_id' => 45, |
| 64 | + 'type' => 'signature', |
| 65 | + 'file_id' => 67, |
| 66 | + 'page' => 2, |
| 67 | + 'urx' => '300', |
| 68 | + 'ury' => '400', |
| 69 | + 'llx' => '100', |
| 70 | + 'lly' => '200', |
| 71 | + 'metadata' => [ 'd' => [ ['w' => 0, 'h' => 800], ['w' => 0, 'h' => 900] ] ], |
| 72 | + ], |
| 73 | + ], |
| 74 | + [ |
| 75 | + 0 => [ 'page' => 2, 'urx' => 300, 'ury' => 400, 'llx' => 100, 'lly' => 200, 'left' => 100, 'top' => 500, 'width' => 200, 'height' => 200 ], |
| 76 | + ], |
| 77 | + ], |
| 78 | + 'multiple elements different sizes' => [ |
| 79 | + [ |
| 80 | + [ |
| 81 | + 'id' => 1, |
| 82 | + 'sign_request_id' => 10, |
| 83 | + 'type' => 'text', |
| 84 | + 'file_id' => 5, |
| 85 | + 'page' => 1, |
| 86 | + 'urx' => 50, |
| 87 | + 'ury' => 150, |
| 88 | + 'llx' => 10, |
| 89 | + 'lly' => 100, |
| 90 | + 'metadata' => [ 'd' => [ ['w' => 0, 'h' => 200] ] ], |
| 91 | + ], |
| 92 | + [ |
| 93 | + 'id' => 2, |
| 94 | + 'sign_request_id' => 11, |
| 95 | + 'type' => 'checkbox', |
| 96 | + 'file_id' => 5, |
| 97 | + 'page' => 1, |
| 98 | + 'urx' => 120, |
| 99 | + 'ury' => 180, |
| 100 | + 'llx' => 100, |
| 101 | + 'lly' => 160, |
| 102 | + 'metadata' => [ 'd' => [ ['w' => 0, 'h' => 200] ] ], |
| 103 | + ], |
| 104 | + ], |
| 105 | + [ |
| 106 | + 0 => [ 'page' => 1, 'width' => 40, 'height' => 50 ], |
| 107 | + 1 => [ 'page' => 1, 'width' => 20, 'height' => 20 ], |
| 108 | + ], |
| 109 | + ], |
| 110 | + 'no metadata fallback uses given dimension' => [ |
| 111 | + [ |
| 112 | + [ |
| 113 | + 'id' => 9, |
| 114 | + 'sign_request_id' => 99, |
| 115 | + 'type' => 'stamp', |
| 116 | + 'file_id' => 8, |
| 117 | + 'page' => 1, |
| 118 | + 'urx' => 200, |
| 119 | + 'ury' => 300, |
| 120 | + 'llx' => 50, |
| 121 | + 'lly' => 100, |
| 122 | + 'metadata' => [ 'd' => [ ['w' => 0, 'h' => 350] ] ], |
| 123 | + ], |
| 124 | + ], |
| 125 | + [ |
| 126 | + 0 => [ 'page' => 1, 'width' => 150, 'height' => 200, 'top' => 50 ], |
| 127 | + ], |
| 128 | + ], |
| 129 | + ]; |
| 130 | + } |
| 131 | +} |
0 commit comments