Add integration tests [MAILPOET-2015]

This commit is contained in:
wxa
2019-07-04 20:20:06 +03:00
committed by M. Shull
parent b174a55d07
commit d36c9b44e4
6 changed files with 161 additions and 4 deletions

View File

@@ -0,0 +1,52 @@
<?php
namespace MailPoet\Test\Subscription;
use Carbon\Carbon;
use Codeception\Util\Fixtures;
use MailPoet\Models\Subscriber;
use MailPoet\Models\SubscriberIP;
use MailPoet\Subscription\Captcha;
use MailPoet\WP\Functions as WPFunctions;
class CaptchaTest extends \MailPoetTest {
function _before() {
$this->captcha = new Captcha(new WPFunctions);
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
}
function testItDoesNotRequireCaptchaForTheFirstSubscription() {
$email = 'non-existent-subscriber@example.com';
$result = $this->captcha->isRequired($email);
expect($result)->equals(false);
}
function testItRequiresCaptchaForRepeatedRecipient() {
$subscriber = Subscriber::create();
$subscriber->hydrate(Fixtures::get('subscriber_template'));
$subscriber->count_confirmations = 1;
$subscriber->save();
$result = $this->captcha->isRequired($subscriber->email);
expect($result)->equals(true);
}
function testItRequiresCaptchaForRepeatedIPAddress() {
$ip = SubscriberIP::create();
$ip->ip = '127.0.0.1';
$ip->created_at = Carbon::now()->subMinutes(1);
$ip->save();
$email = 'non-existent-subscriber@example.com';
$result = $this->captcha->isRequired($email);
expect($result)->equals(true);
}
function testItRendersImage() {
expect_that(empty($_SESSION[Captcha::SESSION_KEY]));
$image = $this->captcha->renderImage(null, null, true);
expect($image)->notEmpty();
expect_that(!empty($_SESSION[Captcha::SESSION_KEY]));
}
function _after() {
SubscriberIP::deleteMany();
}
}

View File

@@ -3,6 +3,8 @@ namespace MailPoet\Test\Subscription;
use Codeception\Stub;
use MailPoet\API\JSON\API;
use MailPoet\API\JSON\ErrorResponse;
use MailPoet\API\JSON\Response;
use MailPoet\DI\ContainerWrapper;
use MailPoet\Form\Util\FieldNameObfuscator;
use MailPoet\Models\Form as FormModel;
@@ -120,6 +122,23 @@ class FormTest extends \MailPoetTest {
expect($result['mailpoet_success'])->null();
}
function testItDoesNotSubscribeAndRedirectsToRedirectUrlIfPresent() {
$redirect_url = 'http://test/';
$url_helper = Stub::make(UrlHelper::class, [
'redirectTo' => function($params) {
return $params;
},
], $this);
$api = Stub::makeEmpty(API::class, [
'processRoute' => function() use ($redirect_url) {
return new ErrorResponse([], ['redirect_url' => $redirect_url], Response::STATUS_BAD_REQUEST);
},
], $this);
$form_controller = new Form($api, $url_helper);
$result = $form_controller->onSubmit($this->request_data);
expect($result)->equals($redirect_url);
}
function _after() {
wp_delete_post($this->post);
\ORM::raw_execute('TRUNCATE ' . SegmentModel::$_table);

View File

@@ -47,7 +47,7 @@ class ThrottlingTest extends \MailPoetTest {
$ip2 = SubscriberIP::create();
$ip2->ip = '127.0.0.1';
$ip2->created_at = Carbon::now()->subDays(1)->subSeconds(1);
$ip2->created_at = Carbon::now()->subMonths(1)->subSeconds(1);
$ip2->save();
expect(SubscriberIP::count())->equals(2);

View File

@@ -16,6 +16,20 @@ class UrlTest extends \MailPoetTest {
$populator->up();
}
function testItReturnsTheCaptchaUrl() {
$url = Url::getCaptchaUrl();
expect($url)->notNull();
expect($url)->contains('action=captcha');
expect($url)->contains('endpoint=subscription');
}
function testItReturnsTheCaptchaImageUrl() {
$url = Url::getCaptchaImageUrl(250, 100);
expect($url)->notNull();
expect($url)->contains('action=captchaImage');
expect($url)->contains('endpoint=subscription');
}
function testItReturnsTheConfirmationUrl() {
// preview
$url = Url::getConfirmationUrl(null);