Skip to content

Commit

Permalink
PHPUnit: New - Initial shared test bases.
Browse files Browse the repository at this point in the history
  • Loading branch information
iNewLegend committed Apr 25, 2021
1 parent 2fa7c9e commit aa37063
Show file tree
Hide file tree
Showing 13 changed files with 865 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

vendor/

.idea/
23 changes: 23 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "inewlegend/elementor-test-base",
"description": "Elementor test base",
"type": "library",
"license": "GPL-3.0",
"minimum-stability": "stable",
"require": {},
"require-dev": {
"justinrainbow/json-schema": "^5.2"
},
"authors": [
{
"name": "Elementor team"
}
],
"autoload": {
"classmap": [
"phpunit/",
"phpunit/factories/",
"phpunit/traits/"
]
}
}
85 changes: 85 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions phpunit/ajax-class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace Elementor\Testing;

use Elementor\Testing\Traits\Base_Elementor;
use Elementor\Testing\Traits\Extra_Assertions;

class Elementor_Test_AJAX extends \WP_Ajax_UnitTestCase {

use Base_Elementor, Extra_Assertions;

public function define_doing_ajax() {
if ( ! wp_doing_ajax() ) {
define( 'DOING_AJAX', true );
}
}

public function _handleAjaxAndDecode( $action ) {
try {
$this->_handleAjax( $action );
} catch ( \WPAjaxDieContinueException $e ) {
unset( $e );
}

return json_decode( $this->_last_response, true );
}
}
27 changes: 27 additions & 0 deletions phpunit/base-class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
namespace Elementor\Testing;

use Elementor\Plugin;
use Elementor\Core\Wp_Api;
use Elementor\Testing\Traits\Auth_Helpers;
use Elementor\Testing\Traits\Base_Elementor;
use Elementor\Testing\Traits\Extra_Assertions;

class Elementor_Test_Base extends \WP_UnitTestCase {
use Base_Elementor, Extra_Assertions, Auth_Helpers;

public function setUp() {
parent::setUp();

set_current_screen( 'dashboard' );
}

public function tearDown() {
parent::tearDown();

Plugin::$instance->editor->set_edit_mode( false );
Plugin::$instance->documents->restore_document();
Plugin::$instance->editor->set_edit_mode( false );
Plugin::$instance->wp = new Wp_Api();
}
}
100 changes: 100 additions & 0 deletions phpunit/base-schema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
namespace Elementor\Testing;

use Elementor\Core\Utils\Collection;
use Elementor\Tracker;
use JsonSchema\Exception\ValidationException;
use JsonSchema\SchemaStorage;
use JsonSchema\Validator;

class Base_Schema extends Elementor_Test_Base {
const HTTP_USER_AGENT = 'test-agent';

public function setUp() {
parent::setUp();

// Required by `Tracker::get_tracking_data`.
$_SERVER['HTTP_USER_AGENT'] = self::HTTP_USER_AGENT;
}

protected function validate_against_schema( $data ) {
// Since the usage system represents objects as array instead of stdClass.
$data = json_decode( json_encode( $data ) );

// Validate
$validator = new Validator;
$validator->validate( $data, (object) [ '$ref' => 'file://' . ELEMENTOR_PATH . 'schemas/usage.json' ] );

if ( ! $validator->isValid() ) {
$error_message = 'JSON does not validate. Violations:' . PHP_EOL;
foreach ( $validator->getErrors() as $error ) {
$error_message .= sprintf( '[%s] %s' . PHP_EOL, $error['property'], $error['message'] );
}

throw new ValidationException( $error_message );
}

return true;
}

protected function validate_current_tracking_data_against_schema() {
return $this->validate_against_schema( Tracker::get_tracking_data() );
}

protected function assert_schema_has_no_additional_properties( $path_to_schema ) {
$json_schema_object = json_decode( file_get_contents( $path_to_schema ) );

$schema_storage = new SchemaStorage();
$schema_storage->addSchema( 'validation', $json_schema_object );

$properties_all_objects_recursive = function ( $node, callable $callback ) use ( &$properties_all_objects_recursive ) {
if ( ! ( $node instanceof \stdClass ) ) {
return;
}

if ( isset( $node->properties ) || isset( $node->patternProperties ) ) {
$callback( $node );
}

foreach ( $node as $part ) {
$properties_all_objects_recursive( $part, $callback );
}
};

// Act.
$usage_schema = $schema_storage->getSchema( 'validation' );

// Assert.
$properties_all_objects_recursive( $usage_schema, function ( $node ) {
$id = $node->{'$id'};
$this->assertTrue( isset( $node->additionalProperties ), "Ensure node: '$id' 'additionalProperties' exists" );
$this->assertFalse( $node->additionalProperties, "Ensure node: '$id' 'additionalProperties' is false" );
} );
}

protected function generate_plugins_mock() {
// Arrange
$plugins = new Collection( [
'elementor/elementor.php' => [
'Elementor tested up to' => '',
'Name' => 'Elementor',
'PluginURI' => 'https:\/\/elementor.com\/?utm_source=wp-plugins&utm_campaign=plugin-uri&utm_medium=wp-dash',
'Version' => ELEMENTOR_VERSION,
'Description' => 'The Elementor Website Builder has it all: drag and drop page builder, pixel perfect design, mobile responsive editing, and more. Get started now!',
'Author' => "Elementor.com",
'AuthorURI' => 'https:\/\/elementor.com\/?utm_source=wp-plugins&utm_campaign=author-uri&utm_medium=wp-dash',
'TextDomain' => 'elementor',
'DomainPath' => '',
'Network' => false,
'RequiresWP' => '',
'RequiresPHP' => '',
'Title' => 'Elementor',
'AuthorName' => 'Elementor.com',
],
] );

$this->mock_wp_api( [
'get_plugins' => $plugins,
] );
}
}
Loading

0 comments on commit aa37063

Please sign in to comment.