PluginProbe
Packeta / trunk
Packeta vtrunk
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / tests / Module / Forms / BugReportFormTest.php

BugReportFormTest.php in Packeta trunk, at tests/Module/Forms/BugReportFormTest.php

210 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare( strict_types=1 );
4
5 namespace Tests\Module\Forms;
6
7 use Packetery\Module\Email\BugReportEmail;
8 use Packetery\Module\Forms\BugReportForm;
9 use Packetery\Module\Framework\WpAdapter;
10 use Packetery\Module\MessageManager;
11 use Packetery\Nette\Forms\Controls\BaseControl;
12 use Packetery\Nette\Forms\Form;
13 use PHPUnit\Framework\MockObject\MockObject;
14 use PHPUnit\Framework\TestCase;
15
16 class BugReportFormTest extends TestCase {
17 private WpAdapter&MockObject $wpAdapter;
18 private MessageManager&MockObject $messageManager;
19 private BugReportEmail&MockObject $bugReportEmail;
20
21 private const TEST_EMAIL = 'test@example.com';
22 private const TEST_SITE_NAME = 'Test Site';
23 private const TEST_MESSAGE = 'Test bug report message';
24 private const ADMIN_EMAIL = 'admin@example.com';
25 private const TEXT_DOMAIN = 'packeta';
26 private const MESSAGE_REQUIRED = 'Message is required.';
27
28 private function createBugReportForm(): BugReportForm {
29 $this->wpAdapter = $this->createMock( WpAdapter::class );
30 $this->messageManager = $this->createMock( MessageManager::class );
31 $this->bugReportEmail = $this->createMock( BugReportEmail::class );
32
33 return new BugReportForm(
34 $this->wpAdapter,
35 $this->messageManager,
36 $this->bugReportEmail
37 );
38 }
39
40 public function testCreateForm(): void {
41 $bugReportForm = $this->createBugReportForm();
42
43 $this->wpAdapter->method( '__' )
44 ->willReturnCallback(
45 function ( $text ) {
46 return $text;
47 }
48 );
49
50 $this->wpAdapter->method( 'getOption' )
51 ->with( 'admin_email' )
52 ->willReturn( self::ADMIN_EMAIL );
53
54 $form = $bugReportForm->createForm();
55
56 $this->assertNotNull( $form->getComponent( 'replyTo' ) );
57 $this->assertNotNull( $form->getComponent( 'message' ) );
58 $this->assertNotNull( $form->getComponent( 'submit' ) );
59 }
60
61 public function testOnFormSuccessWithValidData(): void {
62 $bugReportForm = $this->createBugReportForm();
63
64 $formMock = $this->createMock( Form::class );
65 $values = [
66 'replyTo' => self::TEST_EMAIL,
67 'message' => self::TEST_MESSAGE,
68 ];
69
70 $this->wpAdapter->method( '__' )
71 ->willReturnCallback(
72 function ( $text ) {
73 return $text;
74 }
75 );
76
77 $this->wpAdapter->method( 'sanitizeEmail' )
78 ->with( self::TEST_EMAIL )
79 ->willReturn( self::TEST_EMAIL );
80
81 $this->wpAdapter->method( 'wpKsesPost' )
82 ->with( self::TEST_MESSAGE )
83 ->willReturn( self::TEST_MESSAGE );
84
85 $this->bugReportEmail->method( 'sendBugReport' )
86 ->willReturn( true );
87
88 $this->messageManager->expects( $this->once() )
89 ->method( 'flash_message' )
90 ->with(
91 $this->anything(),
92 MessageManager::TYPE_SUCCESS,
93 MessageManager::RENDERER_PACKETERY,
94 'plugin-options'
95 );
96
97 $bugReportForm->onFormSuccess( $formMock, $values );
98 }
99
100 public function testOnFormSuccessWithFailedEmail(): void {
101 $bugReportForm = $this->createBugReportForm();
102
103 $formMock = $this->createMock( Form::class );
104 $values = [
105 'replyTo' => self::TEST_EMAIL,
106 'message' => self::TEST_MESSAGE,
107 ];
108
109 $this->wpAdapter->method( '__' )
110 ->willReturnCallback(
111 function ( $text ) {
112 return $text;
113 }
114 );
115
116 $this->wpAdapter->method( 'sanitizeEmail' )
117 ->with( self::TEST_EMAIL )
118 ->willReturn( self::TEST_EMAIL );
119
120 $this->wpAdapter->method( 'wpKsesPost' )
121 ->with( self::TEST_MESSAGE )
122 ->willReturn( self::TEST_MESSAGE );
123
124 $this->bugReportEmail->method( 'sendBugReport' )
125 ->willReturn( false );
126
127 $this->messageManager->expects( $this->once() )
128 ->method( 'flash_message' )
129 ->with(
130 $this->anything(),
131 MessageManager::TYPE_ERROR,
132 MessageManager::RENDERER_PACKETERY,
133 'plugin-options'
134 );
135
136 $bugReportForm->onFormSuccess( $formMock, $values );
137 }
138
139 public function testOnFormError(): void {
140 $bugReportForm = $this->createBugReportForm();
141
142 $formMock = $this->createMock( Form::class );
143 $errors = [
144 'email' => 'Email is required.',
145 'message' => 'Message is required.',
146 ];
147
148 $formMock->method( 'getErrors' )
149 ->willReturn( $errors );
150
151 $this->messageManager->expects( $this->exactly( 2 ) )
152 ->method( 'flash_message' )
153 ->with(
154 $this->logicalOr(
155 $this->equalTo( $errors['email'] ),
156 $this->equalTo( $errors['message'] )
157 ),
158 MessageManager::TYPE_ERROR,
159 MessageManager::RENDERER_PACKETERY,
160 'plugin-options'
161 );
162
163 $bugReportForm->onFormError( $formMock );
164 }
165
166 public function testOnFormValidateWithEmptyMessage(): void {
167 $bugReportForm = $this->createBugReportForm();
168
169 $formMock = $this->createMock( Form::class );
170 $formMock->method( 'hasErrors' )
171 ->willReturn( true );
172 $formMock->method( 'getErrors' )
173 ->willReturn( [ self::MESSAGE_REQUIRED ] );
174
175 $this->messageManager
176 ->expects( $this->once() )
177 ->method( 'flash_message' );
178
179 $bugReportForm->onFormValidate( $formMock );
180 }
181
182 public function testOnFormValidateWithValidMessage(): void {
183 $bugReportForm = $this->createBugReportForm();
184
185 $formMock = $this->createMock( Form::class );
186 $messageFieldMock = $this->createMock( BaseControl::class );
187 $validMessage = 'This is a valid message';
188 $values = [
189 'replyTo' => self::TEST_EMAIL,
190 'message' => $validMessage,
191 ];
192
193 $formMock->method( 'getValues' )
194 ->willReturn( $values );
195
196 $formMock->method( 'offsetGet' )
197 ->with( 'message' )
198 ->willReturn( $messageFieldMock );
199
200 $this->wpAdapter->method( 'wpStripAllTags' )
201 ->with( $validMessage )
202 ->willReturn( $validMessage );
203
204 $messageFieldMock->expects( $this->never() )
205 ->method( 'addError' );
206
207 $bugReportForm->onFormValidate( $formMock );
208 }
209 }
210