Bootstrap email-editor unit tests

I also replaced using verify function by asserts, which will be removed in the higher version of phpunit.
[MAILPOET-6216]
This commit is contained in:
Jan Lysý
2024-09-12 18:29:51 +02:00
committed by Jan Lysý
parent 98712db36d
commit d38e6bb1b9
17 changed files with 3838 additions and 89 deletions

3
packages/php/email-editor/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
tests/_output/*
tests/_support/_generated/*
vendor

View File

@ -0,0 +1,18 @@
namespace: Tests
support_namespace: Support
paths:
actor_suffix: Tester
tests: tests
output: tests/_output
data: tests/_data
support: tests/_support
envs: tests/_envs
bootstrap: _bootstrap.php
settings:
colors: true
memory_limit: 1024M
log: true
strict_xml: true
extensions:
enabled:
- Codeception\Extension\RunFailed

View File

@ -4,10 +4,25 @@
"type": "library",
"autoload": {
"psr-4": {
"MailPoet\\EmailEditor\\": "src/"
"MailPoet\\EmailEditor\\": "src/",
"EmailEditor\\": "tests/unit/"
}
},
"require": {
"php": ">=7.4"
},
"require-dev": {
"codeception/codeception": "^4.2",
"codeception/module-asserts": "^2.0",
"phpunit/phpunit": "^9.6"
},
"config": {
"platform": {
"php": "7.4.0"
},
"sort-packages": true
},
"scripts": {
"unit-test": "vendor/bin/codecept run unit"
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,2 @@
*
!.gitignore

View File

@ -0,0 +1,24 @@
<?php
/**
* Inherited Methods
* @method void wantToTest($text)
* @method void wantTo($text)
* @method void execute($callable)
* @method void expectTo($prediction)
* @method void expect($prediction)
* @method void amGoingTo($argumentation)
* @method void am($role)
* @method void lookForwardTo($achieveValue)
* @method void comment($description)
* @method void pause()
*
* @SuppressWarnings(PHPMD)
*/
class UnitTester extends \Codeception\Actor
{
use _generated\UnitTesterActions;
/**
* Define custom actions here
*/
}

View File

@ -0,0 +1,10 @@
# Codeception Test Suite Configuration
#
# Suite for unit or integration tests.
actor: UnitTester
bootstrap: _bootstrap.php
modules:
enabled:
- Asserts
step_decorators: ~

View File

@ -20,10 +20,10 @@ class HighlightingPostprocessorTest extends \MailPoetUnitTest {
<a href="http://example.com">Some <mark style="font-weight:bold;">link</mark></a>
';
$result = $this->postprocessor->postprocess($html);
verify($result)->equals('
$this->assertEquals( '
<span>Some text</span>
<p>Some <span style="color:red;">paragraph</span></p>
<a href="http://example.com">Some <span style="font-weight:bold;">link</span></a>
');
', $result);
}
}

View File

@ -27,6 +27,6 @@ class VariablesPostprocessorTest extends \MailPoetUnitTest {
$this->themeControllerMock->method('getVariablesValuesMap')->willReturn($variablesMap);
$html = '<div style="padding:var(--wp--preset--spacing--10);margin:var(--wp--preset--spacing--20)"><p style="color:white;padding-left:var(--wp--preset--spacing--10);">Helloo I have padding var(--wp--preset--spacing--10); </p></div>';
$result = $this->postprocessor->postprocess($html);
verify($result)->equals('<div style="padding:10px;margin:20px"><p style="color:white;padding-left:10px;">Helloo I have padding var(--wp--preset--spacing--10); </p></div>');
$this->assertEquals('<div style="padding:10px;margin:20px"><p style="color:white;padding-left:10px;">Helloo I have padding var(--wp--preset--spacing--10); </p></div>', $result);
}
}

View File

@ -54,11 +54,11 @@ class BlocksWidthPreprocessorTest extends \MailPoetUnitTest {
$styles['spacing']['padding'] = ['left' => '0px', 'right' => '0px', 'top' => '0px', 'bottom' => '0px'];
$result = $this->preprocessor->preprocess($blocks, $this->layout, $styles);
$result = $result[0];
verify($result['email_attrs']['width'])->equals('660px');
verify($result['innerBlocks'])->arrayCount(3);
verify($result['innerBlocks'][0]['email_attrs']['width'])->equals('330px'); // 660 * 0.5
verify($result['innerBlocks'][1]['email_attrs']['width'])->equals('165px'); // 660 * 0.25
verify($result['innerBlocks'][2]['email_attrs']['width'])->equals('100px');
$this->assertEquals('660px', $result['email_attrs']['width']);
$this->assertCount(3, $result['innerBlocks']);
$this->assertEquals('330px', $result['innerBlocks'][0]['email_attrs']['width']); // 660 * 0.5
$this->assertEquals('165px', $result['innerBlocks'][1]['email_attrs']['width']); // 660 * 0.25
$this->assertEquals('100px', $result['innerBlocks'][2]['email_attrs']['width']);
}
public function testItCalculatesWidthWithLayoutPadding(): void {
@ -91,10 +91,10 @@ class BlocksWidthPreprocessorTest extends \MailPoetUnitTest {
]];
$result = $this->preprocessor->preprocess($blocks, $this->layout, $this->styles);
$result = $result[0];
verify($result['innerBlocks'])->arrayCount(3);
verify($result['innerBlocks'][0]['email_attrs']['width'])->equals('211px'); // (660 - 10 - 10) * 0.33
verify($result['innerBlocks'][1]['email_attrs']['width'])->equals('100px');
verify($result['innerBlocks'][2]['email_attrs']['width'])->equals('128px'); // (660 - 10 - 10) * 0.2
$this->assertCount(3, $result['innerBlocks']);
$this->assertEquals('211px', $result['innerBlocks'][0]['email_attrs']['width']); // (660 - 10 - 10) * 0.33
$this->assertEquals('100px', $result['innerBlocks'][1]['email_attrs']['width']);
$this->assertEquals('128px', $result['innerBlocks'][2]['email_attrs']['width']); // (660 - 10 - 10) * 0.2
}
public function testItCalculatesWidthOfBlockInColumn(): void {
@ -149,11 +149,11 @@ class BlocksWidthPreprocessorTest extends \MailPoetUnitTest {
$result = $this->preprocessor->preprocess($blocks, $this->layout, $this->styles);
$innerBlocks = $result[0]['innerBlocks'];
verify($innerBlocks)->arrayCount(2);
verify($innerBlocks[0]['email_attrs']['width'])->equals('256px'); // (660 - 10 - 10) * 0.4
verify($innerBlocks[0]['innerBlocks'][0]['email_attrs']['width'])->equals('236px'); // 256 - 10 - 10
verify($innerBlocks[1]['email_attrs']['width'])->equals('384px'); // (660 - 10 - 10) * 0.6
verify($innerBlocks[1]['innerBlocks'][0]['email_attrs']['width'])->equals('344px'); // 384 - 25 - 15
$this->assertCount(2, $innerBlocks);
$this->assertEquals('256px', $innerBlocks[0]['email_attrs']['width']); // (660 - 10 - 10) * 0.4
$this->assertEquals('236px', $innerBlocks[0]['innerBlocks'][0]['email_attrs']['width']); // 256 - 10 - 10
$this->assertEquals('384px', $innerBlocks[1]['email_attrs']['width']); // (660 - 10 - 10) * 0.6
$this->assertEquals('344px', $innerBlocks[1]['innerBlocks'][0]['email_attrs']['width']); // 384 - 25 - 15
}
public function testItAddsMissingColumnWidth(): void {
@ -199,13 +199,13 @@ class BlocksWidthPreprocessorTest extends \MailPoetUnitTest {
$result = $this->preprocessor->preprocess($blocks, ['contentSize' => '620px'], $this->styles);
$innerBlocks = $result[0]['innerBlocks'];
verify($innerBlocks)->arrayCount(3);
verify($innerBlocks[0]['email_attrs']['width'])->equals('200px'); // (660 - 10 - 10) * 0.33
verify($innerBlocks[0]['innerBlocks'][0]['email_attrs']['width'])->equals('200px');
verify($innerBlocks[1]['email_attrs']['width'])->equals('200px'); // (660 - 10 - 10) * 0.33
verify($innerBlocks[1]['innerBlocks'][0]['email_attrs']['width'])->equals('200px');
verify($innerBlocks[2]['email_attrs']['width'])->equals('200px'); // (660 - 10 - 10) * 0.33
verify($innerBlocks[2]['innerBlocks'][0]['email_attrs']['width'])->equals('200px');
$this->assertCount(3, $innerBlocks);
$this->assertEquals('200px', $innerBlocks[0]['email_attrs']['width']); // (660 - 10 - 10) * 0.33
$this->assertEquals('200px', $innerBlocks[0]['innerBlocks'][0]['email_attrs']['width']);
$this->assertEquals('200px', $innerBlocks[1]['email_attrs']['width']); // (660 - 10 - 10) * 0.33
$this->assertEquals('200px', $innerBlocks[1]['innerBlocks'][0]['email_attrs']['width']);
$this->assertEquals('200px', $innerBlocks[2]['email_attrs']['width']); // (660 - 10 - 10) * 0.33
$this->assertEquals('200px', $innerBlocks[2]['innerBlocks'][0]['email_attrs']['width']);
}
public function testItCalculatesMissingColumnWidth(): void {
@ -246,10 +246,10 @@ class BlocksWidthPreprocessorTest extends \MailPoetUnitTest {
$result = $this->preprocessor->preprocess($blocks, $this->layout, $this->styles);
$innerBlocks = $result[0]['innerBlocks'];
verify($innerBlocks)->arrayCount(3);
verify($innerBlocks[0]['email_attrs']['width'])->equals('200px'); // (620 - 10 - 10) * 0.3333
verify($innerBlocks[1]['email_attrs']['width'])->equals('200px'); // already defined
verify($innerBlocks[2]['email_attrs']['width'])->equals('200px'); // 600 -200 - 200
$this->assertCount(3, $innerBlocks);
$this->assertEquals('200px', $innerBlocks[0]['email_attrs']['width']); // (620 - 10 - 10) * 0.3333
$this->assertEquals('200px', $innerBlocks[1]['email_attrs']['width']); // already defined
$this->assertEquals('200px', $innerBlocks[2]['email_attrs']['width']); // 600 -200 - 200
}
public function testItDoesNotSubtractPaddingForFullWidthBlocks(): void {
@ -269,9 +269,9 @@ class BlocksWidthPreprocessorTest extends \MailPoetUnitTest {
];
$result = $this->preprocessor->preprocess($blocks, $this->layout, $this->styles);
verify($result)->arrayCount(2);
verify($result[0]['email_attrs']['width'])->equals('660px'); // full width
verify($result[1]['email_attrs']['width'])->equals('640px'); // 660 - 10 - 10
$this->assertCount(2, $result);
$this->assertEquals('660px', $result[0]['email_attrs']['width']); // full width
$this->assertEquals('640px', $result[1]['email_attrs']['width']); // 660 - 10 - 10
}
public function testItCalculatesWidthForColumnWithoutDefinition(): void {
@ -335,10 +335,10 @@ class BlocksWidthPreprocessorTest extends \MailPoetUnitTest {
]];
$result = $this->preprocessor->preprocess($blocks, $this->layout, $this->styles);
verify($result[0]['innerBlocks'])->arrayCount(3);
verify($result[0]['innerBlocks'][0]['email_attrs']['width'])->equals('140px');
verify($result[0]['innerBlocks'][1]['email_attrs']['width'])->equals('220px');
verify($result[0]['innerBlocks'][2]['email_attrs']['width'])->equals('240px');
$this->assertCount(3, $result[0]['innerBlocks']);
$this->assertEquals('140px', $result[0]['innerBlocks'][0]['email_attrs']['width']);
$this->assertEquals('220px', $result[0]['innerBlocks'][1]['email_attrs']['width']);
$this->assertEquals('240px', $result[0]['innerBlocks'][2]['email_attrs']['width']);
$blocks = [[
'blockName' => 'core/columns',
@ -368,9 +368,9 @@ class BlocksWidthPreprocessorTest extends \MailPoetUnitTest {
]];
$result = $this->preprocessor->preprocess($blocks, $this->layout, $this->styles);
verify($result[0]['innerBlocks'])->arrayCount(2);
verify($result[0]['innerBlocks'][0]['email_attrs']['width'])->equals('140px');
verify($result[0]['innerBlocks'][1]['email_attrs']['width'])->equals('500px');
$this->assertCount(2, $result[0]['innerBlocks']);
$this->assertEquals('140px', $result[0]['innerBlocks'][0]['email_attrs']['width']);
$this->assertEquals('500px', $result[0]['innerBlocks'][1]['email_attrs']['width']);
}
public function testItCalculatesWidthForColumnWithBorder(): void {
@ -451,13 +451,13 @@ class BlocksWidthPreprocessorTest extends \MailPoetUnitTest {
]];
$result = $this->preprocessor->preprocess($blocks, $this->layout, $this->styles);
verify($result[0]['innerBlocks'])->arrayCount(3);
verify($result[0]['innerBlocks'][0]['email_attrs']['width'])->equals('140px');
verify($result[0]['innerBlocks'][1]['email_attrs']['width'])->equals('185px');
verify($result[0]['innerBlocks'][2]['email_attrs']['width'])->equals('255px');
$this->assertCount(3, $result[0]['innerBlocks']);
$this->assertEquals('140px', $result[0]['innerBlocks'][0]['email_attrs']['width']);
$this->assertEquals('185px', $result[0]['innerBlocks'][1]['email_attrs']['width']);
$this->assertEquals('255px', $result[0]['innerBlocks'][2]['email_attrs']['width']);
$imageBlock = $result[0]['innerBlocks'][1]['innerBlocks'][0];
verify($imageBlock['email_attrs']['width'])->equals('185px');
$this->assertEquals('185px', $imageBlock['email_attrs']['width']);
$imageBlock = $result[0]['innerBlocks'][2]['innerBlocks'][0];
verify($imageBlock['email_attrs']['width'])->equals('215px');
$this->assertEquals('215px', $imageBlock['email_attrs']['width']);
}
}

View File

@ -45,9 +45,9 @@ class CleanupPreprocessorTest extends \MailPoetUnitTest {
self::PARAGRAPH_BLOCK,
];
$result = $this->preprocessor->preprocess($blocks, $this->layout, $this->styles);
verify($result)->arrayCount(2);
verify($result[0])->equals(self::COLUMNS_BLOCK);
verify($result[1])->equals(self::PARAGRAPH_BLOCK);
$this->assertCount(2, $result);
$this->assertEquals(self::COLUMNS_BLOCK, $result[0]);
$this->assertEquals(self::PARAGRAPH_BLOCK, $result[1]);
}
public function testItPreservesAllRelevantBlocks(): void {
@ -57,9 +57,9 @@ class CleanupPreprocessorTest extends \MailPoetUnitTest {
self::COLUMNS_BLOCK,
];
$result = $this->preprocessor->preprocess($blocks, $this->layout, $this->styles);
verify($result)->arrayCount(3);
verify($result[0])->equals(self::COLUMNS_BLOCK);
verify($result[1])->equals(self::PARAGRAPH_BLOCK);
verify($result[2])->equals(self::COLUMNS_BLOCK);
$this->assertCount(3, $result);
$this->assertEquals(self::COLUMNS_BLOCK, $result[0]);
$this->assertEquals(self::PARAGRAPH_BLOCK, $result[1]);
$this->assertEquals(self::COLUMNS_BLOCK, $result[2]);
}
}

View File

@ -77,18 +77,18 @@ class SpacingPreprocessorTest extends \MailPoetUnitTest {
$expectedEmailAttrs = ['margin-top' => '10px'];
$result = $this->preprocessor->preprocess($blocks, $this->layout, $this->styles);
verify($result)->arrayCount(2);
$this->assertCount(2, $result);
$firstColumns = $result[0];
$secondColumns = $result[1];
// First elements should not have margin-top, but others should.
verify($firstColumns['email_attrs'])->arrayHasNotKey('margin-top');
verify($secondColumns['email_attrs'])->arrayHasKey('margin-top');
verify($secondColumns['email_attrs']['margin-top'])->equals('10px');
$this->assertArrayNotHasKey('margin-top', $firstColumns['email_attrs']);
$this->arrayHasKey('margin-top', $secondColumns['email_attrs']);
$this->assertEquals('10px', $secondColumns['email_attrs']['margin-top']);
// First element children should have margin-top unless first child.
verify($firstColumns['innerBlocks'][0]['email_attrs'])->arrayHasNotKey('margin-top');
verify($firstColumns['innerBlocks'][1]['email_attrs'])->arrayHasKey('margin-top');
verify($firstColumns['innerBlocks'][1]['email_attrs']['margin-top'])->equals('10px');
$this->assertArrayNotHasKey('margin-top', $firstColumns['innerBlocks'][0]['email_attrs']);
$this->assertArrayHasKey('margin-top', $firstColumns['innerBlocks'][1]['email_attrs']);
$this->assertEquals('10px', $firstColumns['innerBlocks'][1]['email_attrs']['margin-top']);
}
}

View File

@ -96,11 +96,11 @@ class TypographyPreprocessorTest extends \MailPoetUnitTest {
];
$result = $this->preprocessor->preprocess($blocks, $this->layout, $this->styles);
$result = $result[0];
verify($result['innerBlocks'])->arrayCount(2);
verify($result['email_attrs'])->equals($expectedEmailAttrs);
verify($result['innerBlocks'][0]['email_attrs'])->equals($expectedEmailAttrs);
verify($result['innerBlocks'][1]['email_attrs'])->equals($expectedEmailAttrs);
verify($result['innerBlocks'][1]['innerBlocks'][0]['email_attrs'])->equals($expectedEmailAttrs);
$this->assertCount(2, $result['innerBlocks']);
$this->assertEquals($expectedEmailAttrs, $result['email_attrs']);
$this->assertEquals($expectedEmailAttrs, $result['innerBlocks'][0]['email_attrs']);
$this->assertEquals($expectedEmailAttrs, $result['innerBlocks'][1]['email_attrs']);
$this->assertEquals($expectedEmailAttrs, $result['innerBlocks'][1]['innerBlocks'][0]['email_attrs']);
}
public function testItReplacesFontSizeSlugsWithValues(): void {
@ -133,11 +133,11 @@ class TypographyPreprocessorTest extends \MailPoetUnitTest {
];
$result = $this->preprocessor->preprocess($blocks, $this->layout, $this->styles);
$result = $result[0];
verify($result['innerBlocks'])->arrayCount(2);
verify($result['email_attrs'])->equals($expectedEmailAttrs);
verify($result['innerBlocks'][0]['email_attrs'])->equals($expectedEmailAttrs);
verify($result['innerBlocks'][1]['email_attrs'])->equals($expectedEmailAttrs);
verify($result['innerBlocks'][1]['innerBlocks'][0]['email_attrs'])->equals($expectedEmailAttrs);
$this->assertCount(2, $result['innerBlocks']);
$this->assertEquals($expectedEmailAttrs, $result['email_attrs']);
$this->assertEquals($expectedEmailAttrs, $result['innerBlocks'][0]['email_attrs']);
$this->assertEquals($expectedEmailAttrs, $result['innerBlocks'][1]['email_attrs']);
$this->assertEquals($expectedEmailAttrs, $result['innerBlocks'][1]['innerBlocks'][0]['email_attrs']);
}
public function testItDoesNotCopyColumnsWidth(): void {
@ -166,12 +166,12 @@ class TypographyPreprocessorTest extends \MailPoetUnitTest {
]];
$result = $this->preprocessor->preprocess($blocks, $this->layout, $this->styles);
$result = $result[0];
verify($result['innerBlocks'])->arrayCount(2);
verify($result['email_attrs'])->equals(['width' => '640px', 'color' => '#000000', 'font-size' => '13px']);
$this->assertCount(2, $result['innerBlocks']);
$this->assertEquals(['width' => '640px', 'color' => '#000000', 'font-size' => '13px'], $result['email_attrs']);
$defaultFontStyles = ['color' => '#000000', 'font-size' => '13px'];
verify($result['innerBlocks'][0]['email_attrs'])->equals($defaultFontStyles);
verify($result['innerBlocks'][1]['email_attrs'])->equals($defaultFontStyles);
verify($result['innerBlocks'][1]['innerBlocks'][0]['email_attrs'])->equals($defaultFontStyles);
$this->assertEquals($defaultFontStyles, $result['innerBlocks'][0]['email_attrs']);
$this->assertEquals($defaultFontStyles, $result['innerBlocks'][1]['email_attrs']);
$this->assertEquals($defaultFontStyles, $result['innerBlocks'][1]['innerBlocks'][0]['email_attrs']);
}
public function testItOverridesColumnsTypography(): void {
@ -262,15 +262,15 @@ class TypographyPreprocessorTest extends \MailPoetUnitTest {
$result = $this->preprocessor->preprocess($blocks, $this->layout, $this->styles);
$child1 = $result[0];
$child2 = $result[1];
verify($child1['innerBlocks'])->arrayCount(2);
verify($child1['email_attrs'])->equals($expectedEmailAttrs1);
verify($child1['innerBlocks'][0]['email_attrs'])->equals($expectedEmailAttrs2);
verify($child1['innerBlocks'][0]['innerBlocks'][0]['email_attrs'])->equals($expectedEmailAttrs2);
verify($child1['innerBlocks'][1]['email_attrs'])->equals($expectedEmailAttrs1);
verify($child1['innerBlocks'][1]['innerBlocks'][0]['email_attrs'])->equals($expectedEmailAttrs1);
verify($child2['innerBlocks'])->arrayCount(1);
verify($child2['email_attrs'])->equals(['color' => '#000000', 'font-size' => '13px']);
verify($child2['innerBlocks'][0]['email_attrs'])->equals($expectedEmailAttrs2);
verify($child2['innerBlocks'][0]['innerBlocks'][0]['email_attrs'])->equals($expectedEmailAttrs2);
$this->assertCount(2, $child1['innerBlocks']);
$this->assertEquals($expectedEmailAttrs1, $child1['email_attrs']);
$this->assertEquals($expectedEmailAttrs2, $child1['innerBlocks'][0]['email_attrs']);
$this->assertEquals($expectedEmailAttrs2, $child1['innerBlocks'][0]['innerBlocks'][0]['email_attrs']);
$this->assertEquals($expectedEmailAttrs1, $child1['innerBlocks'][1]['email_attrs']);
$this->assertEquals($expectedEmailAttrs1, $child1['innerBlocks'][1]['innerBlocks'][0]['email_attrs']);
$this->assertCount(1, $child2['innerBlocks']);
$this->assertEquals(['color' => '#000000', 'font-size' => '13px'], $child2['email_attrs']);
$this->assertEquals($expectedEmailAttrs2, $child2['innerBlocks'][0]['email_attrs']);
$this->assertEquals($expectedEmailAttrs2, $child2['innerBlocks'][0]['innerBlocks'][0]['email_attrs']);
}
}

View File

@ -46,7 +46,7 @@ class ProcessManagerTest extends \MailPoetUnitTest {
$variables->expects($this->once())->method('postprocess')->willReturn('');
$processManager = new ProcessManager($cleanup, $blocksWidth, $typography, $spacing, $highlighting, $variables);
verify($processManager->preprocess([], $layout, $styles))->equals([]);
verify($processManager->postprocess(''))->equals('');
$this->assertEquals([], $processManager->preprocess([], $layout, $styles));
$this->assertEmpty($processManager->postprocess(''));
}
}

View File

@ -30,6 +30,6 @@ class SettingsControllerTest extends \MailPoetUnitTest {
$layoutWidth = $settingsController->getLayoutWidthWithoutPadding();
// default width is 660px and if we subtract padding from left and right we must get the correct value
$expectedWidth = (int)SettingsController::EMAIL_WIDTH - 10 * 2;
verify($layoutWidth)->equals($expectedWidth . 'px');
$this->assertEquals($expectedWidth . 'px', $layoutWidth);
}
}

View File

@ -0,0 +1,16 @@
<?php declare(strict_types = 1);
$console = new \Codeception\Lib\Console\Output([]);
if (!function_exists('esc_attr')) {
function esc_attr($attr) {
return $attr;
}
}
abstract class MailPoetUnitTest extends \Codeception\TestCase\Test {
protected $runTestInSeparateProcess = false;
protected $preserveGlobalState = false;
}
include '_stubs.php';

View File

@ -0,0 +1,14 @@
<?php declare(strict_types = 1);
// phpcs:ignoreFile - We want to allow multiple classes etc.
// Dummy WP classes
if (!class_exists(\WP_Theme_JSON::class)) {
class WP_Theme_JSON {
public function get_data() {
return [];
}
public function get_settings() {
return [];
}
}
}