From d59f2228ccd7a6a603f523c4db66415f39209e53 Mon Sep 17 00:00:00 2001 From: tfrommen Date: Tue, 26 May 2020 09:38:11 +0200 Subject: [PATCH 01/27] Remove the (pass-through!) authentication errors filter --- plugin/inc/auth.php | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/plugin/inc/auth.php b/plugin/inc/auth.php index b2414a0..5035f29 100755 --- a/plugin/inc/auth.php +++ b/plugin/inc/auth.php @@ -1,18 +1,6 @@ Date: Tue, 26 May 2020 09:56:31 +0200 Subject: [PATCH 02/27] Simplify and align namespace usage --- bin/postinstall.sh | 2 +- plugin/Seeder/Command.php | 8 +++----- plugin/Seeder/Generator.php | 8 +++++--- plugin/Seeder/Seeder.php | 10 +++++----- plugin/Seeder/Seeds/Comment.php | 2 -- plugin/Seeder/Seeds/Post.php | 2 -- plugin/Seeder/Seeds/Seed.php | 6 +++--- plugin/Seeder/bootstrap.php | 8 ++++---- 8 files changed, 21 insertions(+), 25 deletions(-) diff --git a/bin/postinstall.sh b/bin/postinstall.sh index 6fa7396..e866c17 100755 --- a/bin/postinstall.sh +++ b/bin/postinstall.sh @@ -18,7 +18,7 @@ if [ ! -d ./seeds ]; then cat > Init.php <seed( $seed_name ); return; @@ -27,7 +25,7 @@ public function seed( $seed_name ) { $seeds_full_path = getcwd() . '/seeds/' . $seed_name . '.php'; if ( ! file_exists( $seeds_full_path ) ) { - \WP_CLI::error( + WP_CLI::error( sprintf( 'There is no "%s" class.', $seed_name ) ); } @@ -38,6 +36,6 @@ public function seed( $seed_name ) { new $seed_name(); $run_time = round( microtime( true ) - $start_time, 2 ); - \WP_CLI::success( 'Seeded ' . $seed_name . ' in ' . $run_time . ' seconds' ); + WP_CLI::success( 'Seeded ' . $seed_name . ' in ' . $run_time . ' seconds' ); } } diff --git a/plugin/Seeder/Generator.php b/plugin/Seeder/Generator.php index 4ad65d9..1d6ebb3 100755 --- a/plugin/Seeder/Generator.php +++ b/plugin/Seeder/Generator.php @@ -2,12 +2,14 @@ namespace WP_Cypress\Seeder; -use WP_Cypress\Seeder\Seeds\Post; use WP_Cypress\Seeder\Seeds\Comment; +use WP_Cypress\Seeder\Seeds\Post; + +use function WP_CLI\Utils\make_progress_bar; class Generator { public function posts( $properties = [], $count = 1 ) { - $progress = \WP_CLI\Utils\make_progress_bar( 'Generating posts', $count ); + $progress = make_progress_bar( 'Generating posts', $count ); $post = new Post( $properties ); @@ -20,7 +22,7 @@ public function posts( $properties = [], $count = 1 ) { } public function comments( $properties = [], $count = 1 ) { - $progress = \WP_CLI\Utils\make_progress_bar( 'Generating comments', $count ); + $progress = make_progress_bar( 'Generating comments', $count ); $comment = new Comment( $properties ); diff --git a/plugin/Seeder/Seeder.php b/plugin/Seeder/Seeder.php index 1ecd01e..e768d64 100755 --- a/plugin/Seeder/Seeder.php +++ b/plugin/Seeder/Seeder.php @@ -2,9 +2,9 @@ namespace WP_Cypress\Seeder; -use \WP_Cypress\Seeder\SeederInterface; -use \WP_Cypress\Seeder\Generator; -use \WP_Cypress\Seeder\Traits\Date; +use Faker\Factory; +use WP_CLI; +use WP_Cypress\Seeder\Traits\Date; abstract class Seeder implements SeederInterface { use Date; @@ -14,13 +14,13 @@ abstract class Seeder implements SeederInterface { protected $faker; public function __construct() { - $this->faker = \Faker\Factory::create(); + $this->faker = Factory::create(); $this->generate = new Generator(); try { $this->run(); } catch ( Exception $e ) { - \WP_CLI::error( $e->getMessage() ); + WP_CLI::error( $e->getMessage() ); } } } diff --git a/plugin/Seeder/Seeds/Comment.php b/plugin/Seeder/Seeds/Comment.php index 2464e79..7dc6d7a 100755 --- a/plugin/Seeder/Seeds/Comment.php +++ b/plugin/Seeder/Seeds/Comment.php @@ -2,8 +2,6 @@ namespace WP_Cypress\Seeder\Seeds; -use WP_Cypress\Seeder\Seeds\Seed; - class Comment extends Seed { public function defaults() { return [ diff --git a/plugin/Seeder/Seeds/Post.php b/plugin/Seeder/Seeds/Post.php index 1a9c3c3..ffed565 100755 --- a/plugin/Seeder/Seeds/Post.php +++ b/plugin/Seeder/Seeds/Post.php @@ -2,8 +2,6 @@ namespace WP_Cypress\Seeder\Seeds; -use WP_Cypress\Seeder\Seeds\Seed; - class Post extends Seed { public function defaults() { $title = $this->faker->sentence(); diff --git a/plugin/Seeder/Seeds/Seed.php b/plugin/Seeder/Seeds/Seed.php index 357614c..ccbe6af 100755 --- a/plugin/Seeder/Seeds/Seed.php +++ b/plugin/Seeder/Seeds/Seed.php @@ -2,8 +2,8 @@ namespace WP_Cypress\Seeder\Seeds; -use \WP_Cypress\Seeder\Seeds\SeedInterface; -use \WP_Cypress\Seeder\Traits\Date; +use Faker\Factory; +use WP_Cypress\Seeder\Traits\Date; abstract class Seed implements SeedInterface { use Date; @@ -13,7 +13,7 @@ abstract class Seed implements SeedInterface { protected $properties = []; public function __construct( $properties ) { - $this->faker = \Faker\Factory::create(); + $this->faker = Factory::create(); $this->properties = $properties; } } diff --git a/plugin/Seeder/bootstrap.php b/plugin/Seeder/bootstrap.php index 184b22e..cca71a0 100755 --- a/plugin/Seeder/bootstrap.php +++ b/plugin/Seeder/bootstrap.php @@ -1,5 +1,8 @@ Date: Tue, 26 May 2020 10:06:37 +0200 Subject: [PATCH 03/27] Use __DIR__ --- plugin/inc/disable-tooltips.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/inc/disable-tooltips.php b/plugin/inc/disable-tooltips.php index 2d1a862..7f4dec4 100755 --- a/plugin/inc/disable-tooltips.php +++ b/plugin/inc/disable-tooltips.php @@ -4,7 +4,7 @@ function disable_tooltips() { add_action( 'enqueue_block_editor_assets', function () { wp_enqueue_script( 'wp-cypress-disable-tooltips', - plugins_url( '/assets/disable-tooltips.js', dirname( __FILE__ ) ), + plugins_url( '/assets/disable-tooltips.js', __DIR__ ), [ 'wp-blocks' ], filemtime( WP_CYPRESS_PLUGIN . '/assets/disable-tooltips.js' ), false From 7c817772744051167311fd22dca652a14ef44d2b Mon Sep 17 00:00:00 2001 From: tfrommen Date: Tue, 26 May 2020 11:04:14 +0200 Subject: [PATCH 04/27] Add parameter type declarations --- plugin/Seeder/Command.php | 2 +- plugin/Seeder/Generator.php | 4 ++-- plugin/Seeder/Seeds/Post.php | 22 +++++++++++++--------- plugin/Seeder/Seeds/Seed.php | 7 ++++--- plugin/inc/Validation.php | 6 +++--- 5 files changed, 23 insertions(+), 18 deletions(-) diff --git a/plugin/Seeder/Command.php b/plugin/Seeder/Command.php index 83fb499..7fe46f1 100755 --- a/plugin/Seeder/Command.php +++ b/plugin/Seeder/Command.php @@ -21,7 +21,7 @@ public function __invoke( $args ) { } } - public function seed( $seed_name ) { + public function seed( string $seed_name ) { $seeds_full_path = getcwd() . '/seeds/' . $seed_name . '.php'; if ( ! file_exists( $seeds_full_path ) ) { diff --git a/plugin/Seeder/Generator.php b/plugin/Seeder/Generator.php index 1d6ebb3..451cd47 100755 --- a/plugin/Seeder/Generator.php +++ b/plugin/Seeder/Generator.php @@ -8,7 +8,7 @@ use function WP_CLI\Utils\make_progress_bar; class Generator { - public function posts( $properties = [], $count = 1 ) { + public function posts( array $properties = [], int $count = 1 ) { $progress = make_progress_bar( 'Generating posts', $count ); $post = new Post( $properties ); @@ -21,7 +21,7 @@ public function posts( $properties = [], $count = 1 ) { $progress->finish(); } - public function comments( $properties = [], $count = 1 ) { + public function comments( array $properties = [], int $count = 1 ) { $progress = make_progress_bar( 'Generating comments', $count ); $comment = new Comment( $properties ); diff --git a/plugin/Seeder/Seeds/Post.php b/plugin/Seeder/Seeds/Post.php index ffed565..c17177b 100755 --- a/plugin/Seeder/Seeds/Post.php +++ b/plugin/Seeder/Seeds/Post.php @@ -35,25 +35,29 @@ public function defaults() { public function generate() { $this->properties = array_merge( $this->defaults(), $this->properties ); - $post = wp_insert_post( $this->properties ); - $this->add_meta( $post ); - $this->add_thumbnail( $post ); + $post_id = wp_insert_post( $this->properties ); + if ( is_wp_error( $post_id ) ) { + return 0; + } + + $this->add_meta( $post_id ); + $this->add_thumbnail( $post_id ); - return $post; + return $post_id; } - public function add_meta( $post ) { + public function add_meta( int $post_id ) { if ( ! isset( $this->properties['post_meta'] ) || ! is_array( $this->properties['post_meta'] ) ) { return; } foreach ( $this->properties['post_meta'] as $key => $value ) { - add_post_meta( $post, $key, $value ); + add_post_meta( $post_id, $key, $value ); } } - public function add_thumbnail( $post ) { + public function add_thumbnail( int $post_id ) { if ( ! isset( $this->properties['post_thumbnail'] ) ) { return; } @@ -65,13 +69,13 @@ public function add_thumbnail( $post ) { ]; if ( ! is_wp_error( $attributes['tmp_name'] ) ) { - $media = media_handle_sideload( $attributes, $post ); + $media = media_handle_sideload( $attributes, $post_id ); if ( is_wp_error( $media ) ) { return; } - set_post_thumbnail( $post, $media ); + set_post_thumbnail( $post_id, $media ); } } } diff --git a/plugin/Seeder/Seeds/Seed.php b/plugin/Seeder/Seeds/Seed.php index ccbe6af..d300ad9 100755 --- a/plugin/Seeder/Seeds/Seed.php +++ b/plugin/Seeder/Seeds/Seed.php @@ -10,10 +10,11 @@ abstract class Seed implements SeedInterface { protected $faker; - protected $properties = []; + protected $properties; - public function __construct( $properties ) { - $this->faker = Factory::create(); + public function __construct( array $properties = [] ) { $this->properties = $properties; + + $this->faker = Factory::create(); } } diff --git a/plugin/inc/Validation.php b/plugin/inc/Validation.php index 0d1555b..6adee7c 100644 --- a/plugin/inc/Validation.php +++ b/plugin/inc/Validation.php @@ -1,7 +1,7 @@ false, 'message' => 'Invalid Theme', From 8254f17929449928f2f8089d0e624ab106ddd483 Mon Sep 17 00:00:00 2001 From: tfrommen Date: Tue, 26 May 2020 11:07:10 +0200 Subject: [PATCH 05/27] Remove unnecessary default --- bin/postinstall.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/postinstall.sh b/bin/postinstall.sh index e866c17..20b5d23 100755 --- a/bin/postinstall.sh +++ b/bin/postinstall.sh @@ -23,7 +23,7 @@ use WP_Cypress\Seeder\Seeder; class Init extends Seeder { public function run() { \$title = \$this->faker->sentence(); - \$this->generate->posts( [ 'post_title' => \$title ], 1 ); + \$this->generate->posts( [ 'post_title' => \$title ] ); } } From e3a3bfa116bcf75486a1f28b3387fc6298a71a05 Mon Sep 17 00:00:00 2001 From: tfrommen Date: Tue, 26 May 2020 11:07:25 +0200 Subject: [PATCH 06/27] Formatting --- plugin/plugin.php | 1 - 1 file changed, 1 deletion(-) diff --git a/plugin/plugin.php b/plugin/plugin.php index 2b7fa66..8973fc7 100755 --- a/plugin/plugin.php +++ b/plugin/plugin.php @@ -23,4 +23,3 @@ disable_auth(); disable_tooltips(); add_debug(); - From 984dd70d8f4000b6cad3390ece817a56d5e37435 Mon Sep 17 00:00:00 2001 From: tfrommen Date: Tue, 26 May 2020 11:09:47 +0200 Subject: [PATCH 07/27] Ensure int, and prevent unnecessary work --- plugin/Seeder/Seeds/Comment.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin/Seeder/Seeds/Comment.php b/plugin/Seeder/Seeds/Comment.php index 7dc6d7a..b313a48 100755 --- a/plugin/Seeder/Seeds/Comment.php +++ b/plugin/Seeder/Seeds/Comment.php @@ -15,9 +15,9 @@ public function defaults() { } public function generate() { - $id = wp_insert_comment( array_merge( $this->defaults(), $this->properties ) ); + $id = (int) wp_insert_comment( array_merge( $this->defaults(), $this->properties ) ); - if ( isset( $this->properties['comment_meta'] ) && is_array( $this->properties['comment_meta'] ) ) { + if ( $id && isset( $this->properties['comment_meta'] ) && is_array( $this->properties['comment_meta'] ) ) { foreach ( $this->properties['comment_meta'] as $key => $value ) { add_comment_meta( $id, $key, $value ); } From bc311e04a275a1cc5393d98ff5f71abfea433180 Mon Sep 17 00:00:00 2001 From: tfrommen Date: Tue, 26 May 2020 11:14:40 +0200 Subject: [PATCH 08/27] Namespace import Exception --- plugin/Seeder/Seeder.php | 1 + 1 file changed, 1 insertion(+) diff --git a/plugin/Seeder/Seeder.php b/plugin/Seeder/Seeder.php index e768d64..009de26 100755 --- a/plugin/Seeder/Seeder.php +++ b/plugin/Seeder/Seeder.php @@ -2,6 +2,7 @@ namespace WP_Cypress\Seeder; +use Exception; use Faker\Factory; use WP_CLI; use WP_Cypress\Seeder\Traits\Date; From 7e48b911bdcdb0b23a3fa2c0fdeadeb0826183c5 Mon Sep 17 00:00:00 2001 From: tfrommen Date: Tue, 26 May 2020 11:16:42 +0200 Subject: [PATCH 09/27] Prevent PHP notice --- plugin/inc/Validation.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/plugin/inc/Validation.php b/plugin/inc/Validation.php index 6adee7c..e1149a1 100644 --- a/plugin/inc/Validation.php +++ b/plugin/inc/Validation.php @@ -54,13 +54,12 @@ public static function validate_theme( string $theme ) { ]; $existing_themes = wp_get_themes( array( 'errors' => null ) ); - $theme = $existing_themes[ $theme ]; - if ( ! isset( $theme ) ) { + if ( ! isset( $existing_themes[ $theme ] ) ) { return $validation; } - $errors = $theme->errors(); + $errors = $existing_themes[ $theme ]->errors(); if ( is_wp_error( $errors ) ) { $validation['message'] = $errors->get_error_message(); From ee4ddf400c00058797f335d72139a60a0c92c4d0 Mon Sep 17 00:00:00 2001 From: tfrommen Date: Tue, 26 May 2020 12:20:09 +0200 Subject: [PATCH 10/27] Allow for dependency injection (also for unit tests) --- plugin/Seeder/Seeder.php | 16 ++++++++++++---- plugin/Seeder/Seeds/Seed.php | 11 +++++++++-- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/plugin/Seeder/Seeder.php b/plugin/Seeder/Seeder.php index 009de26..4d8a0c5 100755 --- a/plugin/Seeder/Seeder.php +++ b/plugin/Seeder/Seeder.php @@ -4,19 +4,27 @@ use Exception; use Faker\Factory; +use Faker\Generator as Faker; use WP_CLI; use WP_Cypress\Seeder\Traits\Date; abstract class Seeder implements SeederInterface { use Date; + /** + * @var Faker + */ + protected $faker; + + /** + * @var Generator + */ protected $generate; - protected $faker; + public function __construct( Generator $generate = null, Faker $faker = null ) { + $this->generate = $generate ?? new Generator(); - public function __construct() { - $this->faker = Factory::create(); - $this->generate = new Generator(); + $this->faker = $faker ?? Factory::create(); try { $this->run(); diff --git a/plugin/Seeder/Seeds/Seed.php b/plugin/Seeder/Seeds/Seed.php index d300ad9..c2b5a2c 100755 --- a/plugin/Seeder/Seeds/Seed.php +++ b/plugin/Seeder/Seeds/Seed.php @@ -3,18 +3,25 @@ namespace WP_Cypress\Seeder\Seeds; use Faker\Factory; +use Faker\Generator as Faker; use WP_Cypress\Seeder\Traits\Date; abstract class Seed implements SeedInterface { use Date; + /** + * @var Faker + */ protected $faker; + /** + * @var array + */ protected $properties; - public function __construct( array $properties = [] ) { + public function __construct( array $properties = [], Faker $faker = null ) { $this->properties = $properties; - $this->faker = Factory::create(); + $this->faker = $faker ?? Factory::create(); } } From b7ceff2e957c1cd0b9d71e4d7b3562887c7afc34 Mon Sep 17 00:00:00 2001 From: tfrommen Date: Tue, 26 May 2020 12:21:28 +0200 Subject: [PATCH 11/27] Formatting --- plugin/inc/auth.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/inc/auth.php b/plugin/inc/auth.php index 5035f29..2d0c5af 100755 --- a/plugin/inc/auth.php +++ b/plugin/inc/auth.php @@ -1,7 +1,7 @@ Date: Tue, 26 May 2020 12:29:55 +0200 Subject: [PATCH 12/27] Add return type declarations --- plugin/Seeder/Seeds/Comment.php | 4 ++-- plugin/Seeder/Seeds/Post.php | 4 ++-- plugin/Seeder/Seeds/SeedInterface.php | 4 ++-- plugin/Seeder/Traits/Date.php | 3 +-- plugin/inc/Validation.php | 10 +++++----- 5 files changed, 12 insertions(+), 13 deletions(-) diff --git a/plugin/Seeder/Seeds/Comment.php b/plugin/Seeder/Seeds/Comment.php index b313a48..d00a1c1 100755 --- a/plugin/Seeder/Seeds/Comment.php +++ b/plugin/Seeder/Seeds/Comment.php @@ -3,7 +3,7 @@ namespace WP_Cypress\Seeder\Seeds; class Comment extends Seed { - public function defaults() { + public function defaults(): array { return [ 'comment_post_ID' => 1, 'comment_author' => 'admin', @@ -14,7 +14,7 @@ public function defaults() { ]; } - public function generate() { + public function generate(): int { $id = (int) wp_insert_comment( array_merge( $this->defaults(), $this->properties ) ); if ( $id && isset( $this->properties['comment_meta'] ) && is_array( $this->properties['comment_meta'] ) ) { diff --git a/plugin/Seeder/Seeds/Post.php b/plugin/Seeder/Seeds/Post.php index c17177b..5a7199a 100755 --- a/plugin/Seeder/Seeds/Post.php +++ b/plugin/Seeder/Seeds/Post.php @@ -3,7 +3,7 @@ namespace WP_Cypress\Seeder\Seeds; class Post extends Seed { - public function defaults() { + public function defaults(): array { $title = $this->faker->sentence(); $slug = sanitize_title( $title ); $now = $this->now(); @@ -33,7 +33,7 @@ public function defaults() { return $defaults; } - public function generate() { + public function generate() : int{ $this->properties = array_merge( $this->defaults(), $this->properties ); $post_id = wp_insert_post( $this->properties ); diff --git a/plugin/Seeder/Seeds/SeedInterface.php b/plugin/Seeder/Seeds/SeedInterface.php index 833277d..b707569 100755 --- a/plugin/Seeder/Seeds/SeedInterface.php +++ b/plugin/Seeder/Seeds/SeedInterface.php @@ -3,7 +3,7 @@ namespace WP_Cypress\Seeder\Seeds; interface SeedInterface { - public function defaults(); + public function defaults(): array; - public function generate(); + public function generate(): int; } diff --git a/plugin/Seeder/Traits/Date.php b/plugin/Seeder/Traits/Date.php index 1599abf..93f8217 100755 --- a/plugin/Seeder/Traits/Date.php +++ b/plugin/Seeder/Traits/Date.php @@ -3,8 +3,7 @@ namespace WP_Cypress\Seeder\Traits; trait Date { - public function now() { + public function now(): string { return current_time( 'Y-m-d H:i:s' ); } } - diff --git a/plugin/inc/Validation.php b/plugin/inc/Validation.php index e1149a1..55ff020 100644 --- a/plugin/inc/Validation.php +++ b/plugin/inc/Validation.php @@ -1,11 +1,11 @@ false, 'message' => 'Invalid plugin', ], (array) $plugin ); @@ -47,7 +47,7 @@ public static function validate_plugins( array $plugins ) { return $validated_plugins; } - public static function validate_theme( string $theme ) { + public static function validate_theme( string $theme ): array { $validation = [ 'valid' => false, 'message' => 'Invalid Theme', @@ -72,7 +72,7 @@ public static function validate_theme( string $theme ) { return $validation; } - public static function active_theme_name() { + public static function active_theme_name(): string { $theme = wp_get_theme(); return $theme->name; } From 495cdcc5be0bf4a7dfd4b0da67d2c32b2c1f8861 Mon Sep 17 00:00:00 2001 From: tfrommen Date: Tue, 26 May 2020 12:37:21 +0200 Subject: [PATCH 13/27] Prevent PHP notice --- plugin/Seeder/Command.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/plugin/Seeder/Command.php b/plugin/Seeder/Command.php index 7fe46f1..fa75a12 100755 --- a/plugin/Seeder/Command.php +++ b/plugin/Seeder/Command.php @@ -8,10 +8,8 @@ class Command { const SEEDS_DIR = 'seeds'; public function __invoke( $args ) { - $seed_name = $args[0]; - - if ( $seed_name ) { - $this->seed( $seed_name ); + if ( ! empty( $args[0] ) ) { + $this->seed( $args[0] ); return; } From 2763900b4b26b2b25fb83ba1e4283e9af861de77 Mon Sep 17 00:00:00 2001 From: tfrommen Date: Tue, 26 May 2020 12:38:18 +0200 Subject: [PATCH 14/27] Use is_readable --- plugin/Seeder/Command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/Seeder/Command.php b/plugin/Seeder/Command.php index fa75a12..9a493fc 100755 --- a/plugin/Seeder/Command.php +++ b/plugin/Seeder/Command.php @@ -22,7 +22,7 @@ public function __invoke( $args ) { public function seed( string $seed_name ) { $seeds_full_path = getcwd() . '/seeds/' . $seed_name . '.php'; - if ( ! file_exists( $seeds_full_path ) ) { + if ( ! is_readable( $seeds_full_path ) ) { WP_CLI::error( sprintf( 'There is no "%s" class.', $seed_name ) ); From b0923feb62b134c48bb43253527a51f6caa53d9c Mon Sep 17 00:00:00 2001 From: tfrommen Date: Tue, 26 May 2020 12:42:34 +0200 Subject: [PATCH 15/27] Fix file loading --- plugin/Seeder/Command.php | 2 +- plugin/inc/debug.php | 2 +- plugin/templates/debug/index.php | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/plugin/Seeder/Command.php b/plugin/Seeder/Command.php index 9a493fc..0af47c7 100755 --- a/plugin/Seeder/Command.php +++ b/plugin/Seeder/Command.php @@ -28,7 +28,7 @@ public function seed( string $seed_name ) { ); } - require_once $seeds_full_path; + include_once $seeds_full_path; $start_time = microtime( true ); new $seed_name(); diff --git a/plugin/inc/debug.php b/plugin/inc/debug.php index 34077fe..f5a42a5 100644 --- a/plugin/inc/debug.php +++ b/plugin/inc/debug.php @@ -6,7 +6,7 @@ function add_debug() { require_once ABSPATH . 'wp-admin/includes/plugin.php'; require_once WP_CYPRESS_PLUGIN . '/inc/Validation.php'; - include_once WP_CYPRESS_PLUGIN . '/templates/debug/index.php'; + require_once WP_CYPRESS_PLUGIN . '/templates/debug/index.php'; die(); } diff --git a/plugin/templates/debug/index.php b/plugin/templates/debug/index.php index 3800e7c..4d31822 100644 --- a/plugin/templates/debug/index.php +++ b/plugin/templates/debug/index.php @@ -1,7 +1,7 @@ - + - +
@@ -150,4 +150,4 @@
- + From a146a3bf3181ea43dbf0165e3588650a1a93115a Mon Sep 17 00:00:00 2001 From: tfrommen Date: Tue, 26 May 2020 12:48:40 +0200 Subject: [PATCH 16/27] Seeder should not need to know about WP-CLI, also explicitly call run() --- plugin/Seeder/Command.php | 11 ++++++++++- plugin/Seeder/Seeder.php | 8 -------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/plugin/Seeder/Command.php b/plugin/Seeder/Command.php index 0af47c7..f2954d7 100755 --- a/plugin/Seeder/Command.php +++ b/plugin/Seeder/Command.php @@ -2,6 +2,7 @@ namespace WP_Cypress\Seeder; +use Exception; use WP_CLI; class Command { @@ -31,7 +32,15 @@ public function seed( string $seed_name ) { include_once $seeds_full_path; $start_time = microtime( true ); - new $seed_name(); + + try { + /** @var SeederInterface $seeder */ + $seeder = new $seed_name(); + $seeder->run(); + } catch ( Exception $e ) { + WP_CLI::error( $e->getMessage() ); + } + $run_time = round( microtime( true ) - $start_time, 2 ); WP_CLI::success( 'Seeded ' . $seed_name . ' in ' . $run_time . ' seconds' ); diff --git a/plugin/Seeder/Seeder.php b/plugin/Seeder/Seeder.php index 4d8a0c5..4046bcd 100755 --- a/plugin/Seeder/Seeder.php +++ b/plugin/Seeder/Seeder.php @@ -2,10 +2,8 @@ namespace WP_Cypress\Seeder; -use Exception; use Faker\Factory; use Faker\Generator as Faker; -use WP_CLI; use WP_Cypress\Seeder\Traits\Date; abstract class Seeder implements SeederInterface { @@ -25,11 +23,5 @@ public function __construct( Generator $generate = null, Faker $faker = null ) { $this->generate = $generate ?? new Generator(); $this->faker = $faker ?? Factory::create(); - - try { - $this->run(); - } catch ( Exception $e ) { - WP_CLI::error( $e->getMessage() ); - } } } From a35f299a8f51654a4c5e01bbdf95aa5d20e7a0af Mon Sep 17 00:00:00 2001 From: tfrommen Date: Tue, 26 May 2020 15:54:50 +0200 Subject: [PATCH 17/27] Use short array syntax --- plugin/inc/Validation.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/inc/Validation.php b/plugin/inc/Validation.php index 55ff020..00afdc2 100644 --- a/plugin/inc/Validation.php +++ b/plugin/inc/Validation.php @@ -53,7 +53,7 @@ public static function validate_theme( string $theme ): array { 'message' => 'Invalid Theme', ]; - $existing_themes = wp_get_themes( array( 'errors' => null ) ); + $existing_themes = wp_get_themes( [ 'errors' => null ] ); if ( ! isset( $existing_themes[ $theme ] ) ) { return $validation; From 9c73aaca9fc869b4aed9924b4675f02d38717dad Mon Sep 17 00:00:00 2001 From: Liam Defty Date: Wed, 27 May 2020 16:32:25 +0100 Subject: [PATCH 18/27] Remove use statement with non-compound name 'WP_CLI', it has no effect --- plugin/Seeder/Command.php | 3 +-- plugin/Seeder/bootstrap.php | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/plugin/Seeder/Command.php b/plugin/Seeder/Command.php index f2954d7..9ec3727 100755 --- a/plugin/Seeder/Command.php +++ b/plugin/Seeder/Command.php @@ -3,7 +3,6 @@ namespace WP_Cypress\Seeder; use Exception; -use WP_CLI; class Command { const SEEDS_DIR = 'seeds'; @@ -21,7 +20,7 @@ public function __invoke( $args ) { } public function seed( string $seed_name ) { - $seeds_full_path = getcwd() . '/seeds/' . $seed_name . '.php'; + $seeds_full_path = getcwd() . '/' . self::SEEDS_DIR . ' /' . $seed_name . '.php'; if ( ! is_readable( $seeds_full_path ) ) { WP_CLI::error( diff --git a/plugin/Seeder/bootstrap.php b/plugin/Seeder/bootstrap.php index cca71a0..f1be010 100755 --- a/plugin/Seeder/bootstrap.php +++ b/plugin/Seeder/bootstrap.php @@ -1,6 +1,5 @@ Date: Wed, 27 May 2020 16:32:54 +0100 Subject: [PATCH 19/27] Fix expected 1 space before opening brace in Post.php --- plugin/Seeder/Seeds/Post.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/Seeder/Seeds/Post.php b/plugin/Seeder/Seeds/Post.php index 5a7199a..f899888 100755 --- a/plugin/Seeder/Seeds/Post.php +++ b/plugin/Seeder/Seeds/Post.php @@ -33,7 +33,7 @@ public function defaults(): array { return $defaults; } - public function generate() : int{ + public function generate() : int { $this->properties = array_merge( $this->defaults(), $this->properties ); $post_id = wp_insert_post( $this->properties ); From 029ed8b1a3e608a051e04650c2482aefd918ece7 Mon Sep 17 00:00:00 2001 From: Liam Defty Date: Wed, 27 May 2020 16:40:23 +0100 Subject: [PATCH 20/27] Refactor trait date to utility functions --- plugin/Seeder/Seeder.php | 3 --- plugin/Seeder/Seeds/Comment.php | 4 +++- plugin/Seeder/Seeds/Post.php | 4 +++- plugin/Seeder/Seeds/Seed.php | 3 --- plugin/Seeder/Traits/Date.php | 9 --------- plugin/Seeder/bootstrap.php | 2 +- plugin/Seeder/utils.php | 7 +++++++ 7 files changed, 14 insertions(+), 18 deletions(-) delete mode 100755 plugin/Seeder/Traits/Date.php create mode 100644 plugin/Seeder/utils.php diff --git a/plugin/Seeder/Seeder.php b/plugin/Seeder/Seeder.php index 4046bcd..2ad929d 100755 --- a/plugin/Seeder/Seeder.php +++ b/plugin/Seeder/Seeder.php @@ -4,11 +4,8 @@ use Faker\Factory; use Faker\Generator as Faker; -use WP_Cypress\Seeder\Traits\Date; abstract class Seeder implements SeederInterface { - use Date; - /** * @var Faker */ diff --git a/plugin/Seeder/Seeds/Comment.php b/plugin/Seeder/Seeds/Comment.php index d00a1c1..f38a843 100755 --- a/plugin/Seeder/Seeds/Comment.php +++ b/plugin/Seeder/Seeds/Comment.php @@ -2,6 +2,8 @@ namespace WP_Cypress\Seeder\Seeds; +use WP_Cypress\Seeder\Utils\now; + class Comment extends Seed { public function defaults(): array { return [ @@ -10,7 +12,7 @@ public function defaults(): array { 'comment_author_email' => 'admin@test.com', 'comment_content' => $this->faker->realText( 100 ), 'user_id' => 1, - 'comment_date' => $this->now(), + 'comment_date' => now(), ]; } diff --git a/plugin/Seeder/Seeds/Post.php b/plugin/Seeder/Seeds/Post.php index f899888..fb795bb 100755 --- a/plugin/Seeder/Seeds/Post.php +++ b/plugin/Seeder/Seeds/Post.php @@ -2,11 +2,13 @@ namespace WP_Cypress\Seeder\Seeds; +use WP_Cypress\Seeder\Utils\now; + class Post extends Seed { public function defaults(): array { $title = $this->faker->sentence(); $slug = sanitize_title( $title ); - $now = $this->now(); + $now = now(); $defaults = [ 'post_author' => 1, 'post_type' => 'post', diff --git a/plugin/Seeder/Seeds/Seed.php b/plugin/Seeder/Seeds/Seed.php index c2b5a2c..8c4f2ff 100755 --- a/plugin/Seeder/Seeds/Seed.php +++ b/plugin/Seeder/Seeds/Seed.php @@ -4,11 +4,8 @@ use Faker\Factory; use Faker\Generator as Faker; -use WP_Cypress\Seeder\Traits\Date; abstract class Seed implements SeedInterface { - use Date; - /** * @var Faker */ diff --git a/plugin/Seeder/Traits/Date.php b/plugin/Seeder/Traits/Date.php deleted file mode 100755 index 93f8217..0000000 --- a/plugin/Seeder/Traits/Date.php +++ /dev/null @@ -1,9 +0,0 @@ - Date: Wed, 27 May 2020 16:50:03 +0100 Subject: [PATCH 21/27] Refactor validation class to namespaced functions --- plugin/Seeder/utils.php | 5 ++ plugin/inc/Validation.php | 135 +++++++++++++++++-------------- plugin/inc/debug.php | 2 +- plugin/templates/debug/index.php | 6 +- 4 files changed, 85 insertions(+), 63 deletions(-) diff --git a/plugin/Seeder/utils.php b/plugin/Seeder/utils.php index 1306eba..1c3f840 100644 --- a/plugin/Seeder/utils.php +++ b/plugin/Seeder/utils.php @@ -2,6 +2,11 @@ namespace WP_Cypress\Seeder\Utils; +/** + * Return the current time, based on the timezone set in WordPress. + * + * @return string + */ function now(): string { return current_time( 'Y-m-d H:i:s' ); } diff --git a/plugin/inc/Validation.php b/plugin/inc/Validation.php index 00afdc2..14c8bec 100644 --- a/plugin/inc/Validation.php +++ b/plugin/inc/Validation.php @@ -1,79 +1,96 @@ false, - 'message' => 'Invalid plugin', - ], (array) $plugin ); - - if ( validate_file( $plugin->name ) ) { - $validation['message'] = 'Invalid plugin path'; - return $validation; - } +namespace WP_Cypress; + +/** + * Validate an array of plugins. + * + * @param array $plugins + * @return array + */ +function validate_plugins( array $plugins ): array { + $installed_plugins = get_plugins(); + + $validated_plugins = array_map( function ( $plugin ) use ( $installed_plugins ): array { + $validation = array_merge( [ + 'valid' => false, + 'message' => 'Invalid plugin', + ], (array) $plugin ); - if ( ! file_exists( WP_PLUGIN_DIR . '/' . $plugin->name ) ) { - $validation['message'] = 'Plugin file does not exist'; - return $validation; - } + if ( validate_file( $plugin->name ) ) { + $validation['message'] = 'Invalid plugin path'; + return $validation; + } - $is_installed = false; + if ( ! file_exists( WP_PLUGIN_DIR . '/' . $plugin->name ) ) { + $validation['message'] = 'Plugin file does not exist'; + return $validation; + } - foreach ( $installed_plugins as $key => $value ) { - if ( preg_match( "/{$plugin->name}/", $key ) ) { - $is_installed = true; - break; - } - } + $is_installed = false; - if ( ! $is_installed ) { - $validation['message'] = 'The plugin does not have a valid header'; - return $validation; + foreach ( $installed_plugins as $key => $value ) { + if ( preg_match( "/{$plugin->name}/", $key ) ) { + $is_installed = true; + break; } + } - $validation['valid'] = true; - $validation['message'] = 'Valid plugin'; - + if ( ! $is_installed ) { + $validation['message'] = 'The plugin does not have a valid header'; return $validation; - }, $plugins ); - - usort ( $validated_plugins, function ( array $left, array $right ) { - return $left['valid'] - $right['valid']; - }); - - return $validated_plugins; - } + } - public static function validate_theme( string $theme ): array { - $validation = [ - 'valid' => false, - 'message' => 'Invalid Theme', - ]; + $validation['valid'] = true; + $validation['message'] = 'Valid plugin'; - $existing_themes = wp_get_themes( [ 'errors' => null ] ); + return $validation; + }, $plugins ); - if ( ! isset( $existing_themes[ $theme ] ) ) { - return $validation; - } + usort ( $validated_plugins, function ( array $left, array $right ) { + return $left['valid'] - $right['valid']; + }); - $errors = $existing_themes[ $theme ]->errors(); + return $validated_plugins; +} - if ( is_wp_error( $errors ) ) { - $validation['message'] = $errors->get_error_message(); - return $validation; - } +/** + * Validate a single theme. + * + * @param string $theme + * @return array + */ +function validate_theme( string $theme ): array { + $validation = [ + 'valid' => false, + 'message' => 'Invalid Theme', + ]; + + $existing_themes = wp_get_themes( [ 'errors' => null ] ); + + if ( ! isset( $existing_themes[ $theme ] ) ) { + return $validation; + } - $validation['valid'] = true; - $validation['message'] = 'Valid theme'; + $errors = $existing_themes[ $theme ]->errors(); + if ( is_wp_error( $errors ) ) { + $validation['message'] = $errors->get_error_message(); return $validation; } - public static function active_theme_name(): string { - $theme = wp_get_theme(); - return $theme->name; - } + $validation['valid'] = true; + $validation['message'] = 'Valid theme'; + + return $validation; +} + +/** + * Return the active theme name. + * + * @return string + */ +function active_theme_name(): string { + $theme = wp_get_theme(); + return $theme->name; } diff --git a/plugin/inc/debug.php b/plugin/inc/debug.php index f5a42a5..f561f7c 100644 --- a/plugin/inc/debug.php +++ b/plugin/inc/debug.php @@ -5,7 +5,7 @@ function add_debug() { $config = json_decode( file_get_contents( ABSPATH . 'config.json' ) ); require_once ABSPATH . 'wp-admin/includes/plugin.php'; - require_once WP_CYPRESS_PLUGIN . '/inc/Validation.php'; + require_once WP_CYPRESS_PLUGIN . '/inc/validation.php'; require_once WP_CYPRESS_PLUGIN . '/templates/debug/index.php'; die(); diff --git a/plugin/templates/debug/index.php b/plugin/templates/debug/index.php index 4d31822..afdda86 100644 --- a/plugin/templates/debug/index.php +++ b/plugin/templates/debug/index.php @@ -30,7 +30,7 @@

Add plugins to the cypress.json configuration.

- plugins ); ?> + plugins ); ?> @@ -71,7 +71,7 @@
Active Theme - +
@@ -91,7 +91,7 @@
name ); ?> - name ); ?> + name ); ?> From f88f566d985bce64f33b37acb0f1fc235209f967 Mon Sep 17 00:00:00 2001 From: Liam Defty Date: Wed, 27 May 2020 17:02:18 +0100 Subject: [PATCH 22/27] Use correct namespace for utils --- plugin/Seeder/Command.php | 3 ++- plugin/Seeder/Seeds/Comment.php | 4 ++-- plugin/Seeder/Seeds/Post.php | 4 ++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/plugin/Seeder/Command.php b/plugin/Seeder/Command.php index 9ec3727..0786f2a 100755 --- a/plugin/Seeder/Command.php +++ b/plugin/Seeder/Command.php @@ -2,6 +2,7 @@ namespace WP_Cypress\Seeder; +use WP_CLI; use Exception; class Command { @@ -20,7 +21,7 @@ public function __invoke( $args ) { } public function seed( string $seed_name ) { - $seeds_full_path = getcwd() . '/' . self::SEEDS_DIR . ' /' . $seed_name . '.php'; + $seeds_full_path = getcwd() . '/' . self::SEEDS_DIR . '/' . $seed_name . '.php'; if ( ! is_readable( $seeds_full_path ) ) { WP_CLI::error( diff --git a/plugin/Seeder/Seeds/Comment.php b/plugin/Seeder/Seeds/Comment.php index f38a843..0611bcc 100755 --- a/plugin/Seeder/Seeds/Comment.php +++ b/plugin/Seeder/Seeds/Comment.php @@ -2,7 +2,7 @@ namespace WP_Cypress\Seeder\Seeds; -use WP_Cypress\Seeder\Utils\now; +use WP_Cypress\Seeder\Utils; class Comment extends Seed { public function defaults(): array { @@ -12,7 +12,7 @@ public function defaults(): array { 'comment_author_email' => 'admin@test.com', 'comment_content' => $this->faker->realText( 100 ), 'user_id' => 1, - 'comment_date' => now(), + 'comment_date' => Utils\now(), ]; } diff --git a/plugin/Seeder/Seeds/Post.php b/plugin/Seeder/Seeds/Post.php index fb795bb..2ca5f07 100755 --- a/plugin/Seeder/Seeds/Post.php +++ b/plugin/Seeder/Seeds/Post.php @@ -2,13 +2,13 @@ namespace WP_Cypress\Seeder\Seeds; -use WP_Cypress\Seeder\Utils\now; +use WP_Cypress\Seeder\Utils; class Post extends Seed { public function defaults(): array { $title = $this->faker->sentence(); $slug = sanitize_title( $title ); - $now = now(); + $now = Utils\now(); $defaults = [ 'post_author' => 1, 'post_type' => 'post', From 04f7de9bbec91db53d389fbfbe5a62e3fc7ad93b Mon Sep 17 00:00:00 2001 From: Liam Defty Date: Wed, 27 May 2020 18:06:31 +0100 Subject: [PATCH 23/27] Rename Validation.php -> validation.php --- plugin/inc/{Validation.php => validation.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename plugin/inc/{Validation.php => validation.php} (100%) diff --git a/plugin/inc/Validation.php b/plugin/inc/validation.php similarity index 100% rename from plugin/inc/Validation.php rename to plugin/inc/validation.php From 23a6505c6d9a4294f9bfad98dcce07e1919b0a8c Mon Sep 17 00:00:00 2001 From: Liam Defty Date: Wed, 27 May 2020 18:07:03 +0100 Subject: [PATCH 24/27] Order namespaces alphabetically --- plugin/Seeder/Command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/Seeder/Command.php b/plugin/Seeder/Command.php index 0786f2a..c78bef2 100755 --- a/plugin/Seeder/Command.php +++ b/plugin/Seeder/Command.php @@ -2,8 +2,8 @@ namespace WP_Cypress\Seeder; -use WP_CLI; use Exception; +use WP_CLI; class Command { const SEEDS_DIR = 'seeds'; From 5389beccfc22c9883589379f1627ec35fef15ab9 Mon Sep 17 00:00:00 2001 From: tfrommen Date: Mon, 8 Jun 2020 13:39:42 +0200 Subject: [PATCH 25/27] Use Composer for autoloading --- plugin/Seeder/bootstrap.php | 8 -------- plugin/composer.json | 9 +++++++++ plugin/composer.lock | 5 +++-- plugin/plugin.php | 10 ++++++++-- 4 files changed, 20 insertions(+), 12 deletions(-) mode change 100755 => 100644 plugin/composer.json mode change 100755 => 100644 plugin/composer.lock diff --git a/plugin/Seeder/bootstrap.php b/plugin/Seeder/bootstrap.php index 15e74d2..d96800c 100755 --- a/plugin/Seeder/bootstrap.php +++ b/plugin/Seeder/bootstrap.php @@ -11,13 +11,5 @@ } require_once __DIR__ . '/utils.php'; -require_once __DIR__ . '/Seeds/SeedInterface.php'; -require_once __DIR__ . '/Seeds/Seed.php'; -require_once __DIR__ . '/Seeds/Post.php'; -require_once __DIR__ . '/Seeds/Comment.php'; -require_once __DIR__ . '/Generator.php'; -require_once __DIR__ . '/SeederInterface.php'; -require_once __DIR__ . '/Seeder.php'; -require_once __DIR__ . '/Command.php'; WP_CLI::add_command( 'seed', Command::class ); diff --git a/plugin/composer.json b/plugin/composer.json old mode 100755 new mode 100644 index 10ee409..af1fd6d --- a/plugin/composer.json +++ b/plugin/composer.json @@ -1,5 +1,14 @@ { + "name": "bigbite/wp-cypress", + "description": "Modifications for a test environment with Cypress.", + "type": "wordpress-plugin", + "license": "MIT", "require": { "fzaninotto/faker": "^1.9" + }, + "autoload": { + "psr-4": { + "WP_Cypress\\Seeder\\": "Seeder/" + } } } diff --git a/plugin/composer.lock b/plugin/composer.lock old mode 100755 new mode 100644 index b15bb8d..5f4b920 --- a/plugin/composer.lock +++ b/plugin/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "4c10bab350777d80b6ec93b084fe5276", + "content-hash": "699c1a8dc0462cdc1354ce9c9264962f", "packages": [ { "name": "fzaninotto/faker", @@ -64,5 +64,6 @@ "prefer-stable": false, "prefer-lowest": false, "platform": [], - "platform-dev": [] + "platform-dev": [], + "plugin-api-version": "1.1.0" } diff --git a/plugin/plugin.php b/plugin/plugin.php index 8973fc7..94b7e18 100755 --- a/plugin/plugin.php +++ b/plugin/plugin.php @@ -10,14 +10,20 @@ namespace WP_Cypress; +use WP_Cypress\Seeder\Command; + if ( ! defined( 'WP_CYPRESS_PLUGIN' ) ) { define( 'WP_CYPRESS_PLUGIN', rtrim( plugin_dir_path( __FILE__ ), '/' ) ); } -require_once WP_CYPRESS_PLUGIN . '/vendor/autoload.php'; +if ( ! class_exists( Command::class ) && is_readable( WP_CYPRESS_PLUGIN . '/vendor/autoload.php' ) ) { + include WP_CYPRESS_PLUGIN . '/vendor/autoload.php'; +} + require_once WP_CYPRESS_PLUGIN . '/inc/auth.php'; -require_once WP_CYPRESS_PLUGIN . '/inc/disable-tooltips.php'; require_once WP_CYPRESS_PLUGIN . '/inc/debug.php'; +require_once WP_CYPRESS_PLUGIN . '/inc/disable-tooltips.php'; + require_once WP_CYPRESS_PLUGIN . '/Seeder/bootstrap.php'; disable_auth(); From ed5ea8cad68a4f07e69ed6b9ef8980afb328aaf3 Mon Sep 17 00:00:00 2001 From: Liam Defty Date: Mon, 6 Jul 2020 14:10:38 +0100 Subject: [PATCH 26/27] Changes to seeder and add default users and ability to switch user - Refactor plugin php - Add default users seeder - Refactor seeds to fixtures - Add ability for user to create fixtures - Allow user to switch user using cy.switchUser command - Change Init seeder to DefaultSeeder - Create the ability to call other seeds in a seeder --- bin/postinstall.sh | 33 -------- example-seeds/DefaultSeeder.php | 15 ++++ example-seeds/ExampleCommentFixture.php | 31 ++++++++ example-seeds/ExampleSeeder.php | 16 ++++ lib/cli/modules/configureWordPress.js | 13 +++- lib/cli/modules/createConfig.js | 14 +++- lib/cypress-support/commands.js | 7 ++ lib/cypress-support/hooks.js | 4 + package.json | 5 +- plugin/Seeder/Command.php | 48 ------------ plugin/Seeder/Generator.php | 36 --------- plugin/Seeder/Seeder.php | 24 ------ plugin/Seeder/Seeds/Seed.php | 24 ------ plugin/Seeder/Seeds/SeedInterface.php | 9 --- plugin/Seeder/bootstrap.php | 15 ---- plugin/composer.json | 2 +- plugin/inc/auth.php | 12 --- plugin/inc/debug.php | 13 ---- plugin/inc/disable-tooltips.php | 13 ---- plugin/plugin.php | 19 ++--- .../Seeds => src/Fixtures}/Comment.php | 20 +++-- plugin/src/Fixtures/Fixture.php | 42 ++++++++++ plugin/src/Fixtures/FixtureInterface.php | 19 +++++ .../{Seeder/Seeds => src/Fixtures}/Post.php | 44 ++++++++--- plugin/src/Fixtures/User.php | 42 ++++++++++ plugin/src/Plugin.php | 77 ++++++++++++++++++ plugin/src/Seeder/SeedCommand.php | 78 +++++++++++++++++++ plugin/src/Seeder/Seeder.php | 37 +++++++++ plugin/{ => src}/Seeder/SeederInterface.php | 0 plugin/src/Seeds/DefaultUsers.php | 29 +++++++ plugin/{Seeder => src}/utils.php | 2 +- plugin/{inc => src}/validation.php | 2 +- plugin/templates/debug/index.php | 14 +++- plugin/templates/debug/partials/hero.php | 1 - update.sh | 2 +- 35 files changed, 488 insertions(+), 274 deletions(-) delete mode 100755 bin/postinstall.sh create mode 100644 example-seeds/DefaultSeeder.php create mode 100644 example-seeds/ExampleCommentFixture.php create mode 100644 example-seeds/ExampleSeeder.php delete mode 100755 plugin/Seeder/Command.php delete mode 100755 plugin/Seeder/Generator.php delete mode 100755 plugin/Seeder/Seeder.php delete mode 100755 plugin/Seeder/Seeds/Seed.php delete mode 100755 plugin/Seeder/Seeds/SeedInterface.php delete mode 100755 plugin/Seeder/bootstrap.php delete mode 100755 plugin/inc/auth.php delete mode 100644 plugin/inc/debug.php delete mode 100755 plugin/inc/disable-tooltips.php rename plugin/{Seeder/Seeds => src/Fixtures}/Comment.php (71%) create mode 100755 plugin/src/Fixtures/Fixture.php create mode 100755 plugin/src/Fixtures/FixtureInterface.php rename plugin/{Seeder/Seeds => src/Fixtures}/Post.php (69%) create mode 100644 plugin/src/Fixtures/User.php create mode 100644 plugin/src/Plugin.php create mode 100755 plugin/src/Seeder/SeedCommand.php create mode 100755 plugin/src/Seeder/Seeder.php rename plugin/{ => src}/Seeder/SeederInterface.php (100%) create mode 100644 plugin/src/Seeds/DefaultUsers.php rename plugin/{Seeder => src}/utils.php (83%) rename plugin/{inc => src}/validation.php (98%) diff --git a/bin/postinstall.sh b/bin/postinstall.sh deleted file mode 100755 index 20b5d23..0000000 --- a/bin/postinstall.sh +++ /dev/null @@ -1,33 +0,0 @@ -#!/bin/bash - -if [ "$INIT_CWD" == "$(pwd)" ]; then - exit 0 -fi - -cd $INIT_CWD - -if [ ! -d ./cypress ]; then - mkdir cypress -fi - -cd cypress - -if [ ! -d ./seeds ]; then - mkdir seeds - cd seeds - cat > Init.php <faker->sentence(); - \$this->generate->posts( [ 'post_title' => \$title ] ); - } -} - -EOF -fi - -exit 0 diff --git a/example-seeds/DefaultSeeder.php b/example-seeds/DefaultSeeder.php new file mode 100644 index 0000000..00b47af --- /dev/null +++ b/example-seeds/DefaultSeeder.php @@ -0,0 +1,15 @@ +create( 3 ); + + $this->call([ + 'ExampleSeeder', + ]); + } +} + diff --git a/example-seeds/ExampleCommentFixture.php b/example-seeds/ExampleCommentFixture.php new file mode 100644 index 0000000..423afb3 --- /dev/null +++ b/example-seeds/ExampleCommentFixture.php @@ -0,0 +1,31 @@ + 1, + 'comment_author' => 'admin', + 'comment_author_email' => 'admin@test.com', + 'comment_content' => 'This is an example comment fixture', + 'user_id' => 1, + 'comment_date' => Utils\now(), + ]; + } + + /** + * Generates a comment record. + * + * @return void + */ + public function generate(): void { + $id = (int) wp_insert_comment( array_merge( $this->defaults(), $this->properties ) ); + } +} diff --git a/example-seeds/ExampleSeeder.php b/example-seeds/ExampleSeeder.php new file mode 100644 index 0000000..e989346 --- /dev/null +++ b/example-seeds/ExampleSeeder.php @@ -0,0 +1,16 @@ + 10, + 'post_title' => 'Post with Custom Comments', + ]) )->create(); + + ( new ExampleCommentFixture( [ 'comment_post_ID' => 10 ] ) )->create( 10 ); + } +} + diff --git a/lib/cli/modules/configureWordPress.js b/lib/cli/modules/configureWordPress.js index 00318a9..a4346e0 100644 --- a/lib/cli/modules/configureWordPress.js +++ b/lib/cli/modules/configureWordPress.js @@ -30,9 +30,16 @@ const configureWordPress = async (config, logFile) => { } await run( - async () => wpcli('seed Init', logFile), - 'Seeding database', - 'Database seeded', + async () => wpcli('seed DefaultUsers', logFile), + 'Creating default users.', + 'Default users created.', + logFile, + ); + + await run( + async () => wpcli('seed DefaultSeeder', logFile), + 'Running default seeder', + 'Default seeder successfully ran.', logFile, ); }; diff --git a/lib/cli/modules/createConfig.js b/lib/cli/modules/createConfig.js index 86e8615..a67df85 100644 --- a/lib/cli/modules/createConfig.js +++ b/lib/cli/modules/createConfig.js @@ -1,3 +1,4 @@ +const fs = require('fs'); const path = require('path'); const shell = require('shelljs'); const glob = require('glob'); @@ -14,6 +15,7 @@ const defaultConfig = { config: {}, port: false, timezone: false, + seedsPath: 'cypress/seeds', }; const createConfig = async (userConfig, dir) => { @@ -22,12 +24,22 @@ const createConfig = async (userConfig, dir) => { ...userConfig, }; + const seedsDir = `${process.cwd()}/${config.seedsPath}`; + + if (!fs.existsSync(seedsDir)) { + shell.mkdir('-p', seedsDir); + + fs.copyFileSync(`${dir}/example-seeds/DefaultSeeder.php`, `${seedsDir}/DefaultSeeder.php`); + fs.copyFileSync(`${dir}/example-seeds/ExampleCommentFixture.php`, `${seedsDir}/ExampleCommentFixture.php`); + fs.copyFileSync(`${dir}/example-seeds/ExampleSeeder.php`, `${seedsDir}/ExampleSeeder.php`); + } + const volumes = [ `${dir}/config.json:/var/www/html/config.json`, `${dir}/config/php.ini:/usr/local/etc/php/php.ini`, `${dir}/config/.htaccess:/var/www/html/.htaccess`, `${dir}/plugin:/var/www/html/wp-content/plugins/wp-cypress`, - `${process.cwd()}/cypress/seeds:/var/www/html/seeds`, + `${seedsDir}:/var/www/html/seeds`, ]; const plugins = []; diff --git a/lib/cypress-support/commands.js b/lib/cypress-support/commands.js index 0017649..d97642c 100644 --- a/lib/cypress-support/commands.js +++ b/lib/cypress-support/commands.js @@ -48,6 +48,13 @@ const commands = { saveCurrentPost() { cy.window().then((win) => win.wp.data.dispatch('core/editor').savePost()); }, + + switchUser(user = 'admin', password = 'password') { + cy.visit('/wp-login.php'); + cy.get('#user_login').clear().type(user); + cy.get('#user_pass').clear().type(password); + cy.get('#wp-submit').click(); + }, }; Object.keys(commands).forEach((command) => { diff --git a/lib/cypress-support/hooks.js b/lib/cypress-support/hooks.js index a25f427..cdd61ab 100644 --- a/lib/cypress-support/hooks.js +++ b/lib/cypress-support/hooks.js @@ -1,6 +1,10 @@ /* eslint-disable no-param-reassign */ let fetchPolyfill; +Cypress.Cookies.defaults({ + whitelist: /wordpress_.*/, +}); + Cypress.wp = {}; before(() => { diff --git a/package.json b/package.json index 4ecf296..9eb56a3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@bigbite/wp-cypress", - "version": "0.5.4", + "version": "0.6.0", "description": "WordPress end to end testing with Cypress.io", "repository": "https://github.com/bigbite/wp-cypress", "author": { @@ -16,8 +16,7 @@ "wp-cypress": "./bin/script.js" }, "scripts": { - "lint:scripts": "eslint \"lib/**/*.js\" \"bin/**/*.js\" \"plugin/**/*.js\"", - "postinstall": "./bin/postinstall.sh" + "lint:scripts": "eslint \"lib/**/*.js\" \"bin/**/*.js\" \"plugin/**/*.js\"" }, "husky": { "hooks": { diff --git a/plugin/Seeder/Command.php b/plugin/Seeder/Command.php deleted file mode 100755 index c78bef2..0000000 --- a/plugin/Seeder/Command.php +++ /dev/null @@ -1,48 +0,0 @@ -seed( $args[0] ); - return; - } - - foreach ( glob( '/' . self::SEEDS_DIR . '/*.php' ) as $filename ) { - $name = pathinfo( $filename, PATHINFO_FILENAME ); - $this->seed( $name ); - } - } - - public function seed( string $seed_name ) { - $seeds_full_path = getcwd() . '/' . self::SEEDS_DIR . '/' . $seed_name . '.php'; - - if ( ! is_readable( $seeds_full_path ) ) { - WP_CLI::error( - sprintf( 'There is no "%s" class.', $seed_name ) - ); - } - - include_once $seeds_full_path; - - $start_time = microtime( true ); - - try { - /** @var SeederInterface $seeder */ - $seeder = new $seed_name(); - $seeder->run(); - } catch ( Exception $e ) { - WP_CLI::error( $e->getMessage() ); - } - - $run_time = round( microtime( true ) - $start_time, 2 ); - - WP_CLI::success( 'Seeded ' . $seed_name . ' in ' . $run_time . ' seconds' ); - } -} diff --git a/plugin/Seeder/Generator.php b/plugin/Seeder/Generator.php deleted file mode 100755 index 451cd47..0000000 --- a/plugin/Seeder/Generator.php +++ /dev/null @@ -1,36 +0,0 @@ -generate(); - $progress->tick(); - } - - $progress->finish(); - } - - public function comments( array $properties = [], int $count = 1 ) { - $progress = make_progress_bar( 'Generating comments', $count ); - - $comment = new Comment( $properties ); - - for ( $i = 1; $i <= $count; $i++ ) { - $comment->generate(); - $progress->tick(); - } - - $progress->finish(); - } -} diff --git a/plugin/Seeder/Seeder.php b/plugin/Seeder/Seeder.php deleted file mode 100755 index 2ad929d..0000000 --- a/plugin/Seeder/Seeder.php +++ /dev/null @@ -1,24 +0,0 @@ -generate = $generate ?? new Generator(); - - $this->faker = $faker ?? Factory::create(); - } -} diff --git a/plugin/Seeder/Seeds/Seed.php b/plugin/Seeder/Seeds/Seed.php deleted file mode 100755 index 8c4f2ff..0000000 --- a/plugin/Seeder/Seeds/Seed.php +++ /dev/null @@ -1,24 +0,0 @@ -properties = $properties; - - $this->faker = $faker ?? Factory::create(); - } -} diff --git a/plugin/Seeder/Seeds/SeedInterface.php b/plugin/Seeder/Seeds/SeedInterface.php deleted file mode 100755 index b707569..0000000 --- a/plugin/Seeder/Seeds/SeedInterface.php +++ /dev/null @@ -1,9 +0,0 @@ - 1, @@ -16,7 +21,12 @@ public function defaults(): array { ]; } - public function generate(): int { + /** + * Generates a comment record. + * + * @return void + */ + public function generate(): void { $id = (int) wp_insert_comment( array_merge( $this->defaults(), $this->properties ) ); if ( $id && isset( $this->properties['comment_meta'] ) && is_array( $this->properties['comment_meta'] ) ) { @@ -24,7 +34,5 @@ public function generate(): int { add_comment_meta( $id, $key, $value ); } } - - return $id; } } diff --git a/plugin/src/Fixtures/Fixture.php b/plugin/src/Fixtures/Fixture.php new file mode 100755 index 0000000..2a96667 --- /dev/null +++ b/plugin/src/Fixtures/Fixture.php @@ -0,0 +1,42 @@ +properties = $properties; + + $this->faker = $faker ?? Factory::create(); + } + + /** + * Generate the specified amount of records. + * + * @param integer $count + * @return void + */ + public function create( int $count = 1 ): void { + for ( $i = 1; $i <= $count; $i++ ) { + $this->generate(); + } + } +} diff --git a/plugin/src/Fixtures/FixtureInterface.php b/plugin/src/Fixtures/FixtureInterface.php new file mode 100755 index 0000000..dbf86e5 --- /dev/null +++ b/plugin/src/Fixtures/FixtureInterface.php @@ -0,0 +1,19 @@ +faker->sentence(); $slug = sanitize_title( $title ); @@ -27,29 +32,40 @@ public function defaults(): array { * Optional post thumbnail, left out of defaults for performance. [ 'post_thumbnail' => [ - 'url' => 'https://unsplash.it/1140/768/?random', - 'name' => str_replace( '.', '', $this->faker->sentence() ), + 'url' => 'https://unsplash.it/1140/768/?random', + 'name' => str_replace( '.', '', $this->faker->sentence() ), + ] ] */ return $defaults; } - public function generate() : int { + /** + * Generates a post record. + * + * @return void + */ + public function generate(): void { $this->properties = array_merge( $this->defaults(), $this->properties ); $post_id = wp_insert_post( $this->properties ); + if ( is_wp_error( $post_id ) ) { - return 0; + return; } $this->add_meta( $post_id ); $this->add_thumbnail( $post_id ); - - return $post_id; } - public function add_meta( int $post_id ) { + /** + * Attaches post meta if provided. + * + * @param integer $post_id + * @return void + */ + public function add_meta( int $post_id ): void { if ( ! isset( $this->properties['post_meta'] ) || ! is_array( $this->properties['post_meta'] ) ) { return; } @@ -59,7 +75,13 @@ public function add_meta( int $post_id ) { } } - public function add_thumbnail( int $post_id ) { + /** + * Downloads, uploads and attaches post thumbnail to post - if provided. + * + * @param integer $post_id + * @return void + */ + public function add_thumbnail( int $post_id ): void { if ( ! isset( $this->properties['post_thumbnail'] ) ) { return; } diff --git a/plugin/src/Fixtures/User.php b/plugin/src/Fixtures/User.php new file mode 100644 index 0000000..edccea3 --- /dev/null +++ b/plugin/src/Fixtures/User.php @@ -0,0 +1,42 @@ + 'password', + 'user_login' => $this->faker->unique()->userName(), + 'user_url' => $this->faker->url(), + 'user_email' => $this->faker->unique()->email(), + 'display_name' => $this->faker->name(), + 'first_name' => $this->faker->firstName(), + 'last_name' => $this->faker->lastName(), + 'post_excerpt' => $this->faker->realText( 200 ), + 'user_registered' => Utils\now(), + 'role' => 'administrator', + ]; + } + + /** + * Generates a new user record. + * + * @return void + */ + public function generate(): void { + $user = wp_insert_user( array_merge( $this->defaults(), $this->properties ) ); + + if ( isset( $this->properties['user_meta'] ) && is_array( $this->properties['user_meta'] ) ) { + foreach ( $this->properties['user_meta'] as $key => $value ) { + add_user_meta( $user, $key, $value ); + } + } + } +} diff --git a/plugin/src/Plugin.php b/plugin/src/Plugin.php new file mode 100644 index 0000000..a7e53ed --- /dev/null +++ b/plugin/src/Plugin.php @@ -0,0 +1,77 @@ +seed( $seeder_name ); + } + + /** + * Validate whether the supplied seeder is a sub class of Seeder. + * + * @param string $seeder_name + * @return void + */ + public function validate_seeder( string $seeder_name ): void { + if ( ! strpos( get_parent_class( $seeder_name ), 'Seeder' ) ) { + WP_CLI::error( + sprintf( '"%s" is not a seeder.', $seeder_name ) + ); + } + } + + /** + * Run an individual seeder. + * + * @param string $seeder_name + * @return void + */ + public function seed( string $seeder_name ): void { + $this->validate_seeder( $seeder_name ); + + $start_time = microtime( true ); + + try { + /** @var SeederInterface $seeder */ + $seeder = new $seeder_name(); + $seeder->run(); + } catch ( Exception $e ) { + WP_CLI::error( $e->getMessage() ); + } + + $run_time = round( microtime( true ) - $start_time, 2 ); + + WP_CLI::success( 'Seeded ' . $seeder_name . ' in ' . $run_time . ' seconds' ); + } +} diff --git a/plugin/src/Seeder/Seeder.php b/plugin/src/Seeder/Seeder.php new file mode 100755 index 0000000..5c1edfb --- /dev/null +++ b/plugin/src/Seeder/Seeder.php @@ -0,0 +1,37 @@ +faker = $faker ?? Factory::create(); + } + + /** + * Run an array of seeders, in the order that they are called. + * + * @param array $seeders + * @return void + */ + public function call( array $seeders ): void { + $command = new SeedCommand(); + + foreach ( $seeders as $seeder ) { + $command( [ $seeder ] ); + } + } + +} diff --git a/plugin/Seeder/SeederInterface.php b/plugin/src/Seeder/SeederInterface.php similarity index 100% rename from plugin/Seeder/SeederInterface.php rename to plugin/src/Seeder/SeederInterface.php diff --git a/plugin/src/Seeds/DefaultUsers.php b/plugin/src/Seeds/DefaultUsers.php new file mode 100644 index 0000000..380d0fc --- /dev/null +++ b/plugin/src/Seeds/DefaultUsers.php @@ -0,0 +1,29 @@ + 'editor', + 'user_login' => 'editor', + ] ) )->create(); + + ( new Fixtures\User( [ + 'role' => 'contributor', + 'user_login' => 'contributor', + ] ) )->create(); + + ( new Fixtures\User( [ + 'role' => 'author', + 'user_login' => 'author', + ] ) )->create(); + + ( new Fixtures\User( [ + 'role' => 'subscriber', + 'user_login' => 'subscriber', + ] ) )->create(); + } +} + diff --git a/plugin/Seeder/utils.php b/plugin/src/utils.php similarity index 83% rename from plugin/Seeder/utils.php rename to plugin/src/utils.php index 1c3f840..a7c32d5 100644 --- a/plugin/Seeder/utils.php +++ b/plugin/src/utils.php @@ -1,6 +1,6 @@ + @@ -30,7 +36,7 @@

Add plugins to the cypress.json configuration.

- plugins ); ?> + plugins ); ?> @@ -71,7 +77,7 @@
Active Theme - +
@@ -91,7 +97,7 @@
name ); ?> - name ); ?> + name ); ?> diff --git a/plugin/templates/debug/partials/hero.php b/plugin/templates/debug/partials/hero.php index 98f2a64..40806e7 100644 --- a/plugin/templates/debug/partials/hero.php +++ b/plugin/templates/debug/partials/hero.php @@ -2,7 +2,6 @@

WP Cypress Debug

- Documentation
diff --git a/update.sh b/update.sh index 7ff72c0..2270161 100644 --- a/update.sh +++ b/update.sh @@ -1,7 +1,7 @@ version=$1 shopt -s extglob -rm -rf !(index.html|seeds|wp-content|update.sh|.htaccess) +rm -rf !(seeds|wp-content|update.sh|.htaccess) cp -rfp ../${version}/* ./ wp --allow-root core version From 837f27d292fa2dfe768493acb19441d8f1dbae33 Mon Sep 17 00:00:00 2001 From: Liam Defty Date: Mon, 6 Jul 2020 14:15:19 +0100 Subject: [PATCH 27/27] Consistency across log messages --- lib/cli/modules/configureWordPress.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/cli/modules/configureWordPress.js b/lib/cli/modules/configureWordPress.js index a4346e0..52e33db 100644 --- a/lib/cli/modules/configureWordPress.js +++ b/lib/cli/modules/configureWordPress.js @@ -31,15 +31,15 @@ const configureWordPress = async (config, logFile) => { await run( async () => wpcli('seed DefaultUsers', logFile), - 'Creating default users.', - 'Default users created.', + 'Creating default users', + 'Default users created', logFile, ); await run( async () => wpcli('seed DefaultSeeder', logFile), 'Running default seeder', - 'Default seeder successfully ran.', + 'Default seeder successfully ran', logFile, ); };