| 1 |
<?php |
| 2 |
|
| 3 |
declare( strict_types=1 ); |
| 4 |
|
| 5 |
namespace Tests\Module\Email; |
| 6 |
|
| 7 |
use Packetery\Latte\Engine; |
| 8 |
use Packetery\Module\Email\BugReportAttachment; |
| 9 |
use Packetery\Module\Email\BugReportEmail; |
| 10 |
use Packetery\Module\Framework\WpAdapter; |
| 11 |
use PHPUnit\Framework\MockObject\MockObject; |
| 12 |
use PHPUnit\Framework\TestCase; |
| 13 |
|
| 14 |
class BugReportEmailTest extends TestCase { |
| 15 |
private WpAdapter&MockObject $wpAdapter; |
| 16 |
private Engine&MockObject $latteEngine; |
| 17 |
private BugReportAttachment&MockObject $bugReportAttachment; |
| 18 |
|
| 19 |
private const TEST_EMAIL = 'test@example.com'; |
| 20 |
private const TEST_SITE_NAME = 'Test Site'; |
| 21 |
private const TEST_MESSAGE = 'Test bug report message'; |
| 22 |
private const TEST_TIMESTAMP = '2024-01-01_12-00-00'; |
| 23 |
private const TEST_EXPORT_CONTENT = 'exported settings content'; |
| 24 |
|
| 25 |
private function createBugReportEmail(): BugReportEmail { |
| 26 |
$this->wpAdapter = $this->createMock( WpAdapter::class ); |
| 27 |
$this->latteEngine = $this->createMock( Engine::class ); |
| 28 |
$this->bugReportAttachment = $this->createMock( BugReportAttachment::class ); |
| 29 |
|
| 30 |
return new BugReportEmail( |
| 31 |
$this->wpAdapter, |
| 32 |
'support@packeta.com', |
| 33 |
$this->latteEngine, |
| 34 |
$this->bugReportAttachment, |
| 35 |
); |
| 36 |
} |
| 37 |
|
| 38 |
public function testSuccessWithSpecialCharacters(): void { |
| 39 |
$bugReportEmail = $this->createBugReportEmail(); |
| 40 |
|
| 41 |
$specialEmail = 'test+tag@example.com'; |
| 42 |
$specialMessage = 'Test message with <script>alert("xss")</script> and special chars: áčďéěíňóřšťúůýž'; |
| 43 |
|
| 44 |
$this->wpAdapter->method( 'sanitizeEmail' ) |
| 45 |
->with( self::TEST_EMAIL ) |
| 46 |
->willReturn( self::TEST_EMAIL ); |
| 47 |
|
| 48 |
$this->wpAdapter->method( 'wpKsesPost' ) |
| 49 |
->with( self::TEST_MESSAGE ) |
| 50 |
->willReturn( self::TEST_MESSAGE ); |
| 51 |
|
| 52 |
$this->wpAdapter->method( 'getBlogInfo' ) |
| 53 |
->with( 'name', 'raw' ) |
| 54 |
->willReturn( self::TEST_SITE_NAME ); |
| 55 |
|
| 56 |
$this->wpAdapter->method( 'currentTime' ) |
| 57 |
->with( 'Y-m-d_H-i-s', false ) |
| 58 |
->willReturn( self::TEST_TIMESTAMP ); |
| 59 |
|
| 60 |
$this->wpAdapter->method( 'wpMail' ) |
| 61 |
->willReturn( true ); |
| 62 |
|
| 63 |
$this->wpAdapter->method( '__' ) |
| 64 |
->willReturnCallback( |
| 65 |
function ( $text ) { |
| 66 |
return $text; |
| 67 |
} |
| 68 |
); |
| 69 |
|
| 70 |
$this->assertTrue( $bugReportEmail->sendBugReport( $specialEmail, $specialMessage, false ) ); |
| 71 |
} |
| 72 |
|
| 73 |
public function testSuccessWithWooCommerceSystemStatus(): void { |
| 74 |
$bugReportEmail = $this->createBugReportEmail(); |
| 75 |
|
| 76 |
$this->wpAdapter->method( 'sanitizeEmail' ) |
| 77 |
->with( self::TEST_EMAIL ) |
| 78 |
->willReturn( self::TEST_EMAIL ); |
| 79 |
|
| 80 |
$this->wpAdapter->method( 'wpKsesPost' ) |
| 81 |
->with( self::TEST_MESSAGE ) |
| 82 |
->willReturn( self::TEST_MESSAGE ); |
| 83 |
|
| 84 |
$this->wpAdapter->method( 'getBlogInfo' ) |
| 85 |
->with( 'name', 'raw' ) |
| 86 |
->willReturn( self::TEST_SITE_NAME ); |
| 87 |
|
| 88 |
$this->wpAdapter->method( 'currentTime' ) |
| 89 |
->with( 'Y-m-d_H-i-s', false ) |
| 90 |
->willReturn( self::TEST_TIMESTAMP ); |
| 91 |
|
| 92 |
$this->wpAdapter->method( 'wpMail' ) |
| 93 |
->willReturn( true ); |
| 94 |
|
| 95 |
$this->wpAdapter->method( '__' ) |
| 96 |
->willReturnCallback( |
| 97 |
function ( $text ) { |
| 98 |
return $text; |
| 99 |
} |
| 100 |
); |
| 101 |
|
| 102 |
$this->assertTrue( $bugReportEmail->sendBugReport( self::TEST_EMAIL, self::TEST_MESSAGE, false ) ); |
| 103 |
} |
| 104 |
|
| 105 |
public function testSuccessWithoutZipArchive(): void { |
| 106 |
$bugReportEmail = $this->createBugReportEmail(); |
| 107 |
|
| 108 |
$this->wpAdapter->method( 'sanitizeEmail' ) |
| 109 |
->with( self::TEST_EMAIL ) |
| 110 |
->willReturn( self::TEST_EMAIL ); |
| 111 |
|
| 112 |
$this->wpAdapter->method( 'wpKsesPost' ) |
| 113 |
->with( self::TEST_MESSAGE ) |
| 114 |
->willReturn( self::TEST_MESSAGE ); |
| 115 |
|
| 116 |
$this->wpAdapter->method( 'getBlogInfo' ) |
| 117 |
->with( 'name', 'raw' ) |
| 118 |
->willReturn( self::TEST_SITE_NAME ); |
| 119 |
|
| 120 |
$this->wpAdapter->method( 'currentTime' ) |
| 121 |
->with( 'Y-m-d_H-i-s', false ) |
| 122 |
->willReturn( self::TEST_TIMESTAMP ); |
| 123 |
|
| 124 |
$this->wpAdapter->method( 'wpMail' ) |
| 125 |
->willReturn( true ); |
| 126 |
|
| 127 |
$this->wpAdapter->method( '__' ) |
| 128 |
->willReturnCallback( |
| 129 |
function ( $text ) { |
| 130 |
return $text; |
| 131 |
} |
| 132 |
); |
| 133 |
|
| 134 |
$this->assertTrue( $bugReportEmail->sendBugReport( self::TEST_EMAIL, self::TEST_MESSAGE, false ) ); |
| 135 |
} |
| 136 |
|
| 137 |
public function testSuccessWithoutWooCommerceSystemStatus(): void { |
| 138 |
$bugReportEmail = $this->createBugReportEmail(); |
| 139 |
|
| 140 |
$this->wpAdapter->method( 'sanitizeEmail' ) |
| 141 |
->with( self::TEST_EMAIL ) |
| 142 |
->willReturn( self::TEST_EMAIL ); |
| 143 |
|
| 144 |
$this->wpAdapter->method( 'wpKsesPost' ) |
| 145 |
->with( self::TEST_MESSAGE ) |
| 146 |
->willReturn( self::TEST_MESSAGE ); |
| 147 |
|
| 148 |
$this->wpAdapter->method( 'getBlogInfo' ) |
| 149 |
->with( 'name', 'raw' ) |
| 150 |
->willReturn( self::TEST_SITE_NAME ); |
| 151 |
|
| 152 |
$this->wpAdapter->method( 'currentTime' ) |
| 153 |
->with( 'Y-m-d_H-i-s', false ) |
| 154 |
->willReturn( self::TEST_TIMESTAMP ); |
| 155 |
|
| 156 |
$this->wpAdapter->method( 'wpMail' ) |
| 157 |
->willReturn( true ); |
| 158 |
|
| 159 |
$this->wpAdapter->method( '__' ) |
| 160 |
->willReturnCallback( |
| 161 |
function ( $text ) { |
| 162 |
return $text; |
| 163 |
} |
| 164 |
); |
| 165 |
|
| 166 |
$this->assertTrue( $bugReportEmail->sendBugReport( self::TEST_EMAIL, self::TEST_MESSAGE, false ) ); |
| 167 |
} |
| 168 |
|
| 169 |
public function testSuccessWithFullZipCreation(): void { |
| 170 |
$bugReportEmail = $this->createBugReportEmail(); |
| 171 |
|
| 172 |
$this->wpAdapter->method( 'sanitizeEmail' ) |
| 173 |
->with( self::TEST_EMAIL ) |
| 174 |
->willReturn( self::TEST_EMAIL ); |
| 175 |
|
| 176 |
$this->wpAdapter->method( 'wpKsesPost' ) |
| 177 |
->with( self::TEST_MESSAGE ) |
| 178 |
->willReturn( self::TEST_MESSAGE ); |
| 179 |
|
| 180 |
$this->wpAdapter->method( 'getBlogInfo' ) |
| 181 |
->with( 'name', 'raw' ) |
| 182 |
->willReturn( self::TEST_SITE_NAME ); |
| 183 |
|
| 184 |
$this->wpAdapter->method( 'currentTime' ) |
| 185 |
->with( 'Y-m-d_H-i-s', false ) |
| 186 |
->willReturn( self::TEST_TIMESTAMP ); |
| 187 |
|
| 188 |
$this->wpAdapter->method( 'wpMail' ) |
| 189 |
->willReturn( true ); |
| 190 |
|
| 191 |
$this->wpAdapter->method( '__' ) |
| 192 |
->willReturnCallback( |
| 193 |
function ( $text ) { |
| 194 |
return $text; |
| 195 |
} |
| 196 |
); |
| 197 |
|
| 198 |
$this->assertTrue( $bugReportEmail->sendBugReport( self::TEST_EMAIL, self::TEST_MESSAGE, false ) ); |
| 199 |
} |
| 200 |
} |
| 201 |
|