first commit

This commit is contained in:
2024-07-15 11:28:08 +02:00
commit f52d538ea5
21891 changed files with 6161164 additions and 0 deletions

View File

@@ -0,0 +1,159 @@
<?php
namespace Elementor\Testing\Factories;
use Elementor\Core\Base\Document;
use Elementor\Plugin;
use WP_UnitTest_Factory_For_Thing;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* @method int create( $args = array(), $generation_definitions = null )
* @method Document create_and_get( $args = array(), $generation_definitions = null )
* @method int[] create_many( $count, $args = array(), $generation_definitions = null )
*/
class Documents extends WP_UnitTest_Factory_For_Thing {
const DEFAULT_WIDGET_DATA_MOCK = [
'id' => 'mock-widget',
'elType' => 'widget',
'isInner' => false,
'settings' => [ 'text' => 'I\'m just a mock', ],
'elements' => [],
'widgetType' => 'button',
];
const DEFAULT_DYNAMIC_WIDGET_DATA_MOCK = [
'id' => 'mock-widget-dynamic',
'elType' => 'widget',
'settings' => [
'title' => 'Add Your Heading Text Here',
'link' => [
'custom_attributes' => '',
'is_external' => '',
'nofollow' => '',
'url' => 'http://just-a-mock.com',
],
'__dynamic__' => [
'title' => '[elementor-tag id="2e7ade9" name="post-title" settings="%7B%7D"]',
'link' => '[elementor-tag id="68a0003" name="post-url" settings="%7B%7D"]',
],
],
'elements' => [],
'widgetType' => 'heading',
];
const DEFAULT_DOCUMENT_DATA_MOCK = [
[
'id' => 'mock-section',
'elType' => 'section',
'isInner' => false,
'settings' => [
'post_status' => 'new',
],
'elements' => [
[
'id' => 'mock-column',
'elType' => 'column',
'isInner' => false,
'settings' => [ '_column_size' => 100, ],
'elements' => [ self::DEFAULT_WIDGET_DATA_MOCK ],
],
],
],
];
const DOCUMENT_DATA_MOCK_WITHOUT_ELEMENTS = [
'settings' => [
'post_status' => 'publish',
],
'elements' => [],
];
const DOCUMENT_DATA_MOCK_WITH_DYNAMIC_WIDGET = [
[
'id' => 'mock-section',
'elType' => 'section',
'isInner' => false,
'settings' => [
'post_status' => 'new',
],
'elements' => [
[
'id' => 'mock-column',
'elType' => 'column',
'isInner' => false,
'settings' => [ '_column_size' => 100, ],
'elements' => [ self::DEFAULT_DYNAMIC_WIDGET_DATA_MOCK ],
],
],
],
];
public function __construct( $factory = null ) {
parent::__construct( $factory );
$this->default_generation_definitions = [
'post_title' => new \WP_UnitTest_Generator_Sequence( 'Elementor post title %s' ),
'post_content' => new \WP_UnitTest_Generator_Sequence( 'Elementor post content %s' ),
'post_excerpt' => new \WP_UnitTest_Generator_Sequence( 'Elementor post excerpt %s' ),
'post_type' => 'post',
];
}
public function create_object( $args ) {
$type = 'page';
$meta = [];
if ( isset( $args['post_type'] ) ) {
$type = $args['post_type'];
unset( $args['post_type'] );
}
if ( isset( $args['meta_input'] ) ) {
$meta = $args['meta_input'];
unset( $args['meta_input'] );
}
if ( empty( $meta['_elementor_data'] ) ) {
$meta['_elementor_data'] = wp_json_encode( self::DEFAULT_DOCUMENT_DATA_MOCK );
}
return Plugin::$instance->documents->create( $type, $args, $meta )->get_id();
}
public function update_object( $document_id, $fields ) {
$fields['ID'] = $document_id;
wp_update_post( $fields );
return Plugin::$instance->documents->get( $document_id, false );
}
public function get_object_by_id( $document_id ) {
return Plugin::$instance->documents->get( $document_id, false );
}
/**
* publish_and_get.
*
* Why this method exists and called the way it do?
*
* Naming: called the way it do to follow PHPUnit convection.
* Why publish after create? It have to behave in the same way it do in real-world.
* elementor never create a document that already published ( Other system depends on this behavior).
*
* @param array $args
*
* @return \Elementor\Core\Base\Document|false
*/
public function publish_and_get( $args = [] ) {
$document = $this->create_and_get( $args );
return $this->update_object( $document->get_id(), [ 'post_status' => 'publish' ] );
}
}

View File

@@ -0,0 +1,172 @@
<?php
namespace Elementor\Testing\Factories;
use Elementor\Plugin;
use Elementor\Core\Base\Document;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Factory extends \WP_UnitTest_Factory {
/**
* @var Documents
*/
public $documents;
/**
* Factory constructor.
*/
public function __construct() {
parent::__construct();
$this->documents = new Documents();
}
/**
* @return \WP_Post
*/
public function get_default_post() {
return $this->post->create_and_get();
}
/**
* @return \WP_Post
*/
public function create_and_get_default_post() {
return $this->post->create_and_get();
}
/**
* @param array $args
*
* @return \WP_Post
*/
public function get_custom_post( $args ) {
$custom_post = clone $this->get_default_post();
$this->post->update_object( $custom_post->ID, $args );
return $custom_post;
}
/**
* @param $args
*
* @return array|null|\WP_Post
*/
public function create_and_get_custom_post( $args ) {
return $this->post->create_and_get( $args );
}
/**
* @return array parent_id | int; child_id | int; user_id | int.
*/
public function create_and_get_parent_and_child_posts() {
$user_id = $this->user->create( [ 'display_name' => 'elementor' ] );
$post_id = $this->post->create(
[
'post_author' => $user_id,
'post_date' => '2014-11-11 23:45:30',
'post_type' => 'revision',
]
);
$inherent_post_id = $this->post->create(
[
'post_date' => '2014-11-12 23:45:30',
'post_type' => 'revision',
'post_author' => $user_id,
'post_parent' => $post_id,
'post_name' => $post_id . '-autosave',
]
);
return [
'parent_id' => $post_id,
'child_id' => $inherent_post_id,
'user_id' => $user_id,
];
}
/**
* @return int|\WP_Error
*/
public function get_local_template_id() {
return $this->create_template();
}
/**
* create new template and return it.
*
* @param $rags
*
* @return int|\WP_Error template id
*/
public function create_and_get_template_id( $rags ) {
return $this->create_template( $rags );
}
/**
* @return \WP_User
*/
public function create_and_get_administrator_user() {
return $this->user->create_and_get( [ 'role' => 'administrator' ] );
}
/**
* @return \WP_User
*/
public function get_administrator_user() {
return $this->user->create_and_get( [ 'role' => 'administrator' ] );
}
/**
* @return \WP_User
*/
public function get_subscriber_user() {
return $this->user->create_and_get( [ 'role' => 'subscriber' ] );
}
/**
* @return \WP_User
*/
public function get_editor_user() {
return $this->user->create_and_get( [ 'role' => 'editor' ] );
}
/**
* @return Document|false
*/
public function create_post() {
$admin = $this->create_and_get_administrator_user();
wp_set_current_user( $admin->ID );
$post = $this->create_and_get_custom_post( [
'post_author' => $admin->ID,
'post_type' => 'post',
] );
add_post_meta( $post->ID, '_elementor_edit_mode', 'builder' );
$document = Plugin::$instance->documents->get( $post->ID );
return $document;
}
private function create_template( $template_data = [ 'title' => 'new template' ] ) {
$template_id = $this->post->create(
[
'post_title' => ! empty( $template_data['title'] ) ? $template_data['title'] : __( '(no title)', 'elementor' ),
'post_status' => current_user_can( 'publish_posts' ) ? 'publish' : 'pending',
'post_type' => \Elementor\TemplateLibrary\Source_Local::CPT,
'post_content' => '<ul><li title="Edit">Edit</li></ul><h3>This is the heading</h3>Lorem ipsum dolor sit amet consectetur adipiscing elit dolor<h3>This is the heading</h3>Lorem ipsum dolor sit amet consectetur adipiscing elit dolor<a href="#">Click Here</a>',
]
);
update_post_meta( $template_id, '_elementor_data', '["type":"bla"]' );
return $template_id;
}
}