]> git.agnieray.net Git - galette.git/blob - tests/Galette/Core/tests/units/MailingHistory.php
Improve coding standards
[galette.git] / tests / Galette / Core / tests / units / MailingHistory.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\Core\tests\units;
23
24 use Galette\GaletteTestCase;
25
26 /**
27 * Mailing history tests class
28 *
29 * @author Johan Cwiklinski <johan@x-tnd.be>
30 */
31 class MailingHistory extends GaletteTestCase
32 {
33 protected int $seed = 20240131082138;
34
35 /**
36 * Cleanup after each test method
37 *
38 * @return void
39 */
40 public function tearDown(): void
41 {
42 parent::tearDown();
43
44 $this->zdb = new \Galette\Core\Db();
45 $delete = $this->zdb->delete(\Galette\Core\MailingHistory::TABLE);
46 $this->zdb->execute($delete);
47 }
48
49 /**
50 * Test history workflow
51 *
52 * @return void
53 */
54 public function testHistoryFlow(): void
55 {
56 $this->logSuperAdmin();
57 $mh = new \Galette\Core\MailingHistory(
58 $this->zdb,
59 $this->login,
60 $this->preferences
61 );
62
63 //nothing in the logs at the beginning
64 $list = $mh->getHistory();
65 $this->assertCount(0, $list);
66
67 $filters = new \Galette\Filters\MembersList();
68 $adh1 = $this->getMemberOne();
69 $adh2 = $this->getMemberTwo();
70 $filters->selected = [$adh1->id, $adh2->id];
71
72 $m = new \Galette\Repository\Members();
73 $members = $m->getArrayList($filters->selected);
74 $mailing = new \Galette\Core\Mailing($this->preferences, $members);
75
76 $mailing->subject = 'Test mailing';
77 $mailing->body = 'This is a test mailing';
78 $mailing->sender = [
79 'name' => 'Galette unit tests',
80 'email' => 'test@galette.eu'
81 ];
82 $mailing->current_step = \Galette\Core\Mailing::STEP_SEND;
83
84 $mh = new \Galette\Core\MailingHistory(
85 $this->zdb,
86 $this->login,
87 $this->preferences,
88 null,
89 $mailing
90 );
91 //user store mailing request (not send yet)
92 $this->assertTrue($mh->storeMailing());
93
94 //one entry in the logs
95 $list = $mh->getHistory();
96 $this->assertCount(1, $list);
97
98 $entry = $list[0];
99 $this->assertSame('Test mailing', $entry->mailing_subject);
100 $this->assertCount(2, $entry->mailing_recipients);
101 $this->assertEquals(0, $entry->mailing_sent);
102 $this->assertSame(0, $entry->attachments);
103
104 $first_not_sent_id = $entry->mailing_id;
105
106 $mailing = new \Galette\Core\Mailing($this->preferences, $members);
107 $mh = new \Galette\Core\MailingHistory(
108 $this->zdb,
109 $this->login,
110 $this->preferences,
111 null,
112 $mailing
113 );
114 $this->assertTrue($mh::loadFrom($this->zdb, $first_not_sent_id, $mailing, false));
115
116 $this->assertSame('Test mailing', $mailing->subject);
117 $this->assertCount(2, $entry->mailing_recipients);
118 $this->assertEquals(0, $entry->mailing_sent);
119 $this->assertSame(0, $entry->attachments);
120
121 //change and store again (still not send yet)
122 $mailing->subject = 'Test mailing (changed)';
123 $this->assertTrue($mh->storeMailing());
124
125 //still one entry in the logs
126 $list = $mh->getHistory();
127 $this->assertCount(1, $list);
128
129 $entry = $list[0];
130 $second_not_sent_id = $entry->mailing_id;
131 $this->assertSame('Test mailing (changed)', $entry->mailing_subject);
132 $this->assertCount(2, $entry->mailing_recipients);
133 $this->assertEquals(0, $entry->mailing_sent);
134 $this->assertSame(0, $entry->attachments);
135 $this->assertSame($first_not_sent_id, $second_not_sent_id);
136
137 $mailing = new \Galette\Core\Mailing($this->preferences, $members);
138 $mh = new \Galette\Core\MailingHistory(
139 $this->zdb,
140 $this->login,
141 $this->preferences,
142 null,
143 $mailing
144 );
145 $this->assertTrue($mh::loadFrom($this->zdb, $second_not_sent_id, $mailing, false));
146
147 //store "sent" mailing
148 $this->assertTrue($mh->storeMailing(true));
149
150 //still one entry in the logs
151 $list = $mh->getHistory();
152 $this->assertCount(1, $list);
153
154 $entry = $list[0];
155 $this->assertSame('Test mailing (changed)', $entry->mailing_subject);
156 $this->assertCount(2, $entry->mailing_recipients);
157 $this->assertEquals(1, $entry->mailing_sent);
158 $this->assertSame(0, $entry->attachments);
159 $this->assertSame($second_not_sent_id, $entry->mailing_id);
160
161 //add antoher mailing in history
162 $mailing = new \Galette\Core\Mailing($this->preferences, $members);
163
164 $mailing->subject = 'Filter subject test';
165 $mailing->body = 'This is a test mailing for filters';
166 $mailing->sender = [
167 'name' => 'Galette admin unit tests',
168 'email' => 'test+admin@galette.eu'
169 ];
170 $mailing->current_step = \Galette\Core\Mailing::STEP_SEND;
171
172 $filters = new \Galette\Filters\MailingsList();
173 $mh = new \Galette\Core\MailingHistory(
174 $this->zdb,
175 $this->login,
176 $this->preferences,
177 $filters,
178 $mailing
179 );
180 //user store mailing request (not send yet)
181 $this->assertTrue($mh->storeMailing());
182
183 //still one entry in the logs
184 $list = $mh->getHistory();
185 $this->assertCount(2, $list);
186
187 $filters->subject_filter = 'filter';
188 $list = $mh->getHistory();
189 $this->assertCount(1, $list);
190
191 $filters->subject_filter = null;
192 $filters->sent_filter = \Galette\Core\MailingHistory::FILTER_SENT;
193 $list = $mh->getHistory();
194 $this->assertCount(1, $list);
195
196 $entry = $list[0];
197 $this->assertSame('Test mailing (changed)', $entry->mailing_subject);
198 $this->assertCount(2, $entry->mailing_recipients);
199 $this->assertEquals(1, $entry->mailing_sent);
200 }
201 }