|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * OpenMage |
| 5 | + * |
| 6 | + * This source file is subject to the Open Software License (OSL 3.0) |
| 7 | + * that is bundled with this package in the file LICENSE.txt. |
| 8 | + * It is also available at https://opensource.org/license/osl-3-0-php |
| 9 | + * |
| 10 | + * @category OpenMage |
| 11 | + * @package OpenMage_Tests |
| 12 | + * @copyright Copyright (c) 2025 The OpenMage Contributors (https://www.openmage.org) |
| 13 | + * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
| 14 | + */ |
| 15 | + |
| 16 | +declare(strict_types=1); |
| 17 | + |
| 18 | +namespace OpenMage\Tests\Unit\Mage\Contacts\Controllers; |
| 19 | + |
| 20 | +use Generator; |
| 21 | +use Mage; |
| 22 | +use Mage_Contacts_IndexController as Subject; |
| 23 | +use PHPUnit\Framework\TestCase; |
| 24 | + |
| 25 | +class IndexControllerTest extends TestCase |
| 26 | +{ |
| 27 | + protected function setUp(): void |
| 28 | + { |
| 29 | + Mage::app(); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @dataProvider postActionDataProvider |
| 34 | + * @group Mage_Contacts |
| 35 | + * @group Mage_Contacts_Controller |
| 36 | + * @runInSeparateProcess |
| 37 | + */ |
| 38 | + public function testPostAction(array $postData, bool $isFormKeyValid, ?string $expectedErrorMessage): void |
| 39 | + { |
| 40 | + $requestMock = $this->getMockBuilder(\Mage_Core_Controller_Request_Http::class) |
| 41 | + ->disableOriginalConstructor() |
| 42 | + ->onlyMethods(['getPost']) |
| 43 | + ->getMock(); |
| 44 | + $requestMock->method('getPost')->willReturn($postData); |
| 45 | + |
| 46 | + $subject = $this->getMockBuilder(Subject::class) |
| 47 | + ->disableOriginalConstructor() |
| 48 | + ->onlyMethods(['_validateFormKey', 'getRequest', '_redirect']) |
| 49 | + ->getMock(); |
| 50 | + $subject->method('getRequest')->willReturn($requestMock); |
| 51 | + $subject->method('_validateFormKey')->willReturn($isFormKeyValid); |
| 52 | + |
| 53 | + $sessionMock = $this->getMockBuilder(\Mage_Customer_Model_Session::class) |
| 54 | + ->disableOriginalConstructor() |
| 55 | + ->onlyMethods(['addError', 'addSuccess']) |
| 56 | + ->getMock(); |
| 57 | + |
| 58 | + Mage::register('_singleton/customer/session', $sessionMock); |
| 59 | + |
| 60 | + if ($expectedErrorMessage) { |
| 61 | + $sessionMock->expects($this->once()) |
| 62 | + ->method('addError') |
| 63 | + ->with($this->equalTo($expectedErrorMessage)); |
| 64 | + } else { |
| 65 | + $sessionMock->expects($this->once()) |
| 66 | + ->method('addSuccess') |
| 67 | + ->with($this->equalTo('Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.')); |
| 68 | + } |
| 69 | + |
| 70 | + $subject->expects($this->once())->method('_redirect')->with('*/*/'); |
| 71 | + $subject->postAction(); |
| 72 | + |
| 73 | + Mage::unregister('_singleton/customer/session'); |
| 74 | + } |
| 75 | + |
| 76 | + public function postActionDataProvider(): Generator |
| 77 | + { |
| 78 | + $validData = [ |
| 79 | + 'name' => 'John Doe', |
| 80 | + |
| 81 | + 'comment' => 'Test comment', |
| 82 | + ]; |
| 83 | + |
| 84 | + yield 'valid data' => [ |
| 85 | + $validData, |
| 86 | + true, |
| 87 | + null, |
| 88 | + ]; |
| 89 | + |
| 90 | + yield 'invalid form key' => [ |
| 91 | + $validData, |
| 92 | + false, |
| 93 | + 'Invalid Form Key. Please submit your request again.', |
| 94 | + ]; |
| 95 | + |
| 96 | + $data = $validData; |
| 97 | + $data['name'] = ''; |
| 98 | + yield 'missing name' => [ |
| 99 | + $data, |
| 100 | + true, |
| 101 | + 'Unable to submit your request. Please, try again later', |
| 102 | + ]; |
| 103 | + |
| 104 | + $data = $validData; |
| 105 | + $data['email'] = ''; |
| 106 | + yield 'missing email' => [ |
| 107 | + $data, |
| 108 | + true, |
| 109 | + 'Unable to submit your request. Please, try again later', |
| 110 | + ]; |
| 111 | + |
| 112 | + $data = $validData; |
| 113 | + $data['email'] = 'invalid-email'; |
| 114 | + yield 'invalid email' => [ |
| 115 | + $data, |
| 116 | + true, |
| 117 | + 'Unable to submit your request. Please, try again later', |
| 118 | + ]; |
| 119 | + |
| 120 | + $data = $validData; |
| 121 | + $data['comment'] = ''; |
| 122 | + yield 'missing comment' => [ |
| 123 | + $data, |
| 124 | + true, |
| 125 | + 'Unable to submit your request. Please, try again later', |
| 126 | + ]; |
| 127 | + } |
| 128 | +} |
0 commit comments