Skip to content

Commit 6749a20

Browse files
committed
Tests - détection des images non utilisées et expirées
1 parent fefced6 commit 6749a20

File tree

2 files changed

+161
-5
lines changed

2 files changed

+161
-5
lines changed

__tests/AdminTest.php

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
3+
/*
4+
* Copyright 2008-2025 Anael MOBILIA
5+
*
6+
* This file is part of image-heberg.fr.
7+
*
8+
* image-heberg.fr 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+
* image-heberg.fr 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 image-heberg.fr. If not, see <http://www.gnu.org/licenses/>
20+
*/
21+
22+
namespace ImageHebergTests;
23+
24+
use ImageHeberg\HelperAbuse;
25+
use ImageHeberg\HelperAdmin;
26+
use ImageHeberg\ImageObject;
27+
use ImageHeberg\RessourceObject;
28+
use ImageHeberg\UtilisateurObject;
29+
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
30+
use PHPUnit\Framework\TestCase;
31+
32+
class AdminTest extends TestCase
33+
{
34+
/**
35+
* Fichiers qui n'ont jamais été utilisés
36+
*/
37+
#[RunInSeparateProcess]
38+
public function testGetNeverUsedFiles(): void
39+
{
40+
require 'config/config.php';
41+
42+
$filesToDelete = HelperAdmin::getNeverUsedFiles();
43+
44+
$this->assertNotNull(
45+
$filesToDelete->offsetGet(36),
46+
'neverUsedFileWithoutThumbs.png doit être détectée'
47+
);
48+
$this->assertNotNull(
49+
$filesToDelete->offsetGet(37),
50+
'neverUsedFileWithThumbsNotDisplayed.png doit être détectée'
51+
);
52+
$this->assertNull(
53+
$filesToDelete->offsetGet(38),
54+
'neverUsedFileWithThumbsDisplayed.png ne doit pas être détectée (miniatures affichées récemment)'
55+
);
56+
$this->assertNull(
57+
$filesToDelete->offsetGet(39),
58+
'neverUsedFileWithoutThumbsButOwned.png ne doit pas être détectée (image possédée par un utilisateur)'
59+
);
60+
$this->assertNull(
61+
$filesToDelete->offsetGet(40),
62+
'expiredFileWithoutThumbs.png ne doit pas être détectée (image utilisée)'
63+
);
64+
$this->assertNull(
65+
$filesToDelete->offsetGet(41),
66+
'expiredFileWithThumbsNotBetter.png ne doit pas être détectée (image utilisée)'
67+
);
68+
$this->assertNull(
69+
$filesToDelete->offsetGet(42),
70+
'expiredFileWithThumbsDisplayedRecently.png ne doit pas être détectée (image utilisée)'
71+
);
72+
$this->assertNull(
73+
$filesToDelete->offsetGet(43),
74+
'expiredFileWithoutThumbsAndOwned.png ne doit pas être détectée (image utilisée)'
75+
);
76+
$this->assertNull(
77+
$filesToDelete->offsetGet(44),
78+
'neverUsedFileWithThumbsDisplayedLongTimeAgo.png ne doit pas être détectée (miniature déjà affichée)'
79+
);
80+
}
81+
82+
/**
83+
* Fichier qui ne sont plus utilisés selon les règles de conservation
84+
*/
85+
#[RunInSeparateProcess]
86+
public function testGetUnusedFiles(): void
87+
{
88+
require 'config/config.php';
89+
90+
$filesToDelete = HelperAdmin::getUnusedFiles();
91+
92+
$this->assertNull(
93+
$filesToDelete->offsetGet(36),
94+
'neverUsedFileWithoutThumbs.png ne doit pas être détectée (non concernée par ce test)'
95+
);
96+
$this->assertNull(
97+
$filesToDelete->offsetGet(37),
98+
'neverUsedFileWithThumbsNotDisplayed.png ne doit pas être détectée (non concernée par ce test)'
99+
);
100+
$this->assertNull(
101+
$filesToDelete->offsetGet(38),
102+
'neverUsedFileWithThumbsDisplayed.png ne doit pas être détectée (non concernée par ce test)'
103+
);
104+
$this->assertNull(
105+
$filesToDelete->offsetGet(39),
106+
'neverUsedFileWithoutThumbsButOwned.png ne doit pas être détectée (non concernée par ce test)'
107+
);
108+
$this->assertNotNull(
109+
$filesToDelete->offsetGet(40),
110+
'expiredFileWithoutThumbs.png doit être détectée'
111+
);
112+
$this->assertNotNull(
113+
$filesToDelete->offsetGet(41),
114+
'expiredFileWithThumbsNotBetter.png doit être détectée'
115+
);
116+
$this->assertNull(
117+
$filesToDelete->offsetGet(42),
118+
'expiredFileWithThumbsDisplayedRecently.png ne doit pas être détectée (miniature utilisée)'
119+
);
120+
$this->assertNull(
121+
$filesToDelete->offsetGet(43),
122+
'expiredFileWithoutThumbsAndOwned.png ne doit pas être détectée (image possédée)'
123+
);
124+
$this->assertNotNull(
125+
$filesToDelete->offsetGet(44),
126+
'neverUsedFileWithThumbsDisplayedLongTimeAgo.png doit être détectée (miniature affichée il y a trop longtemps)'
127+
);
128+
}
129+
}

__tests/data.sql

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,19 @@ INSERT INTO `images` (`id`, `remote_addr`, `remote_port`, `date_action`, `old_na
107107
(35, '127.0.0.1', 1234, NOW(), 'imageDejaValidee.png', 'image_35.png', 1, 1, 1, NOW(), 10, 10, 'not-used--ba9cb5a0afba67138bd328', 0, 0, 1, '127.0.0', '');
108108

109109
--
110+
-- Images pour le calcul des suppressions
111+
--
112+
INSERT INTO `images` (`id`, `remote_addr`, `remote_port`, `date_action`, `old_name`, `new_name`, `size`, `height`, `width`, `last_view`, `nb_view_v4`, `nb_view_v6`, `md5`, `isBloquee`, `isSignalee`, `isApprouvee`, `abuse_network`, `abuse_categorie`) VALUES
113+
(36, '127.0.0.1', 1234, DATE_SUB(CURRENT_DATE(), INTERVAL 8 DAY), 'neverUsedFileWithoutThumbs.png', 'image_36.png', 1, 1, 1, 0000-00-00, 0, 0, 'not-used--ba9cb5a0afba67138bd336', 0, 0, 0, '127.0.0', ''),
114+
(37, '127.0.0.1', 1234, DATE_SUB(CURRENT_DATE(), INTERVAL 8 DAY), 'neverUsedFileWithThumbsNotDisplayed.png', 'image_37.png', 1, 1, 1, 0000-00-00, 0, 0, 'not-used--ba9cb5a0afba67138bd337', 0, 0, 0, '127.0.0', ''),
115+
(38, '127.0.0.1', 1234, DATE_SUB(CURRENT_DATE(), INTERVAL 8 DAY), 'neverUsedFileWithThumbsDisplayed.png', 'image_38.png', 1, 1, 1, 0000-00-00, 0, 0, 'not-used--ba9cb5a0afba67138bd338', 0, 0, 0, '127.0.0', ''),
116+
(39, '127.0.0.1', 1234, DATE_SUB(CURRENT_DATE(), INTERVAL 8 DAY), 'neverUsedFileWithoutThumbsButOwned.png', 'image_39.png', 1, 1, 1, 0000-00-00, 0, 0, 'not-used--ba9cb5a0afba67138bd339', 0, 0, 0, '127.0.0', ''),
117+
(40, '127.0.0.1', 1234, '2024-01-01', 'expiredFileWithoutThumbs.png', 'image_40.png', 1, 1, 1, DATE_SUB(CURRENT_DATE(), INTERVAL 365 DAY), 1, 0, 'not-used--ba9cb5a0afba67138bd340', 0, 0, 0, '127.0.0', ''),
118+
(41, '127.0.0.1', 1234, '2024-01-01', 'expiredFileWithThumbsNotBetter.png', 'image_41.png', 1, 1, 1, DATE_SUB(CURRENT_DATE(), INTERVAL 365 DAY), 1, 0, 'not-used--ba9cb5a0afba67138bd341', 0, 0, 0, '127.0.0', ''),
119+
(42, '127.0.0.1', 1234, '2024-01-01', 'expiredFileWithThumbsDisplayedRecently.png', 'image_42.png', 1, 1, 1, DATE_SUB(CURRENT_DATE(), INTERVAL 365 DAY), 1, 0, 'not-used--ba9cb5a0afba67138bd342', 0, 0, 0, '127.0.0', ''),
120+
(43, '127.0.0.1', 1234, '2024-01-01', 'expiredFileWithoutThumbsAndOwned.png', 'image_43.png', 1, 1, 1, DATE_SUB(CURRENT_DATE(), INTERVAL 365 DAY), 1, 0, 'not-used--ba9cb5a0afba67138bd343', 0, 0, 0, '127.0.0', ''),
121+
(44, '127.0.0.1', 1234, DATE_SUB(CURRENT_DATE(), INTERVAL 8 DAY), 'neverUsedFileWithThumbsDisplayedLongTimeAgo.png', 'image_44.png', 1, 1, 1, 0000-00-00, 0, 0, 'not-used--ba9cb5a0afba67138bd344', 0, 0, 0, '127.0.0', '');
122+
--
110123
-- Agrandir la taille du champ pour bien gérer le _bootstrap
111124
--
112125
ALTER TABLE `thumbnails` MODIFY `new_name` VARCHAR(50) ;
@@ -121,15 +134,29 @@ INSERT INTO `thumbnails` (`id`, `images_id`, `date_action`, `new_name`, `size`,
121134
--
122135
INSERT INTO `thumbnails` (`id`, `images_id`, `date_action`, `new_name`, `size`, `height`, `width`, `last_view`, `nb_view_v4`, `nb_view_v6`, `md5`) VALUES
123136
(3, 20, '2023-01-01', '14777777.png', 10316, 100, 100, '2023-01-01', 999999999999, 999999999999, 'not-used--f12d4a42776aba3a16761e');
124-
137+
--
138+
-- Miniatures pour le calcul des suppressions
139+
--
140+
INSERT INTO `thumbnails` (`id`, `images_id`, `date_action`, `new_name`, `size`, `height`, `width`, `last_view`, `nb_view_v4`, `nb_view_v6`, `md5`) VALUES
141+
(4, 37, '2024-01-01', 'neverUsedFileWithThumbsNotDisplayed.png', 10316, 100, 100, '00-00-00', 0, 0, 'not-used--ba9cb5a0afba67138bd337'),
142+
(5, 38, '2024-01-01', 'neverUsedFileWithThumbsDisplayed', 10316, 100, 100, NOW(), 10, 10, 'not-used--ba9cb5a0afba67138bd338'),
143+
(6, 41, '2024-01-01', 'expiredFileWithThumbsNotBetter.png', 10316, 100, 100, DATE_SUB(CURRENT_DATE(), INTERVAL 366 DAY), 10, 10, 'not-used--ba9cb5a0afba67138bd341'),
144+
(7, 42, '2024-01-01', 'expiredFileWithThumbsDisplayedRecently.png', 10316, 100, 100, NOW(), 10, 10, 'not-used--ba9cb5a0afba67138bd342'),
145+
(8, 44, '2024-01-01', 'neverUsedFileWithThumbsDisplayedLongTimeAgo.png', 10316, 100, 100, DATE_SUB(CURRENT_DATE(), INTERVAL 366 DAY), 10, 10, 'not-used--ba9cb5a0afba67138bd344');
125146

126147
--
127148
-- Possessions
128149
--
129-
INSERT INTO `possede` (`images_id`, `membres_id`) VALUES ('11', '2'),
130-
('14', '1'),
131-
('99', '2');
132-
150+
INSERT INTO `possede` (`images_id`, `membres_id`) VALUES
151+
('11', '2'),
152+
('14', '1'),
153+
('99', '2');
154+
--
155+
-- Possessions pour le calcul des suppressions
156+
--
157+
INSERT INTO `possede` (`images_id`, `membres_id`) VALUES
158+
('39', '2'),
159+
('43', '2');
133160

134161
--
135162
-- Second compte utilisateur

0 commit comments

Comments
 (0)