Skip to content

Commit

Permalink
Merge branch 'refs/heads/develop' into fix/issue-1489-valueerror-clas…
Browse files Browse the repository at this point in the history
…s-log

# Conflicts:
#	tests/bootstrap.php
  • Loading branch information
delawski committed Jul 19, 2024
2 parents f9a22f5 + a303ca6 commit 26ac1ba
Show file tree
Hide file tree
Showing 39 changed files with 793 additions and 665 deletions.
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,12 @@
"@composer validate",
"@lint-php"
],
"lint-tests": [
"phpcs ./tests --standard=./tests/phpcs.xml.dist"
],
"format": [
"phpcbf ."
"phpcbf .",
"phpcbf ./tests --standard=./tests/phpcs.xml.dist"
],
"test": [
"phpunit --coverage-text",
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"lint": "npm-run-all lint:*",
"lint:js": "eslint .",
"lint:php": "composer lint",
"lint:php-tests": "composer lint-tests",
"format": "composer format",
"test": "npm-run-all test:*",
"test:php": "npm run cli -- composer test --working-dir=wp-content/plugins/stream-src",
Expand Down
39 changes: 25 additions & 14 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
<?php
/**
* PHPUnit bootstrap file
*
* @package WP_Stream
*/

// Defined in docker-compose.yml for the container running the tests.
$_tests_dir = getenv( 'WP_TESTS_DIR' );

if ( empty( $_tests_dir ) || ! file_exists( $_tests_dir . '/includes' ) ) {
trigger_error( 'Unable to locate WP_TESTS_DIR', E_USER_ERROR );
trigger_error( 'Unable to locate WP_TESTS_DIR', E_USER_ERROR ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
}

// Use in code to trigger custom actions.
Expand All @@ -23,7 +28,7 @@
* @param array $active_plugins
* @return array
*/
function xwp_filter_active_plugins_for_phpunit( $active_plugins ) {
function wp_stream_filter_active_plugins_for_phpunit( $active_plugins ) {
$forced_active_plugins = array();
if ( defined( 'WP_TEST_ACTIVATED_PLUGINS' ) ) {
$forced_active_plugins = preg_split( '/\s*,\s*/', WP_TEST_ACTIVATED_PLUGINS );
Expand All @@ -36,12 +41,12 @@ function xwp_filter_active_plugins_for_phpunit( $active_plugins ) {
}
return $active_plugins;
}
tests_add_filter( 'site_option_active_sitewide_plugins', 'xwp_filter_active_plugins_for_phpunit' );
tests_add_filter( 'option_active_plugins', 'xwp_filter_active_plugins_for_phpunit' );
tests_add_filter( 'site_option_active_sitewide_plugins', 'wp_stream_filter_active_plugins_for_phpunit' );
tests_add_filter( 'option_active_plugins', 'wp_stream_filter_active_plugins_for_phpunit' );

tests_add_filter(
'muplugins_loaded',
function() {
function () {
// Manually load the plugin.
require dirname( __DIR__ ) . '/stream.php';

Expand All @@ -54,32 +59,38 @@ function() {
/**
* Manually loads Mercator for testing.
*/
function xwp_manually_load_mercator() {
function wp_stream_manually_load_mercator() {
define( 'MERCATOR_SKIP_CHECKS', true );
require WPMU_PLUGIN_DIR . '/mercator/mercator.php';
}
tests_add_filter( 'muplugins_loaded', 'xwp_manually_load_mercator' );
tests_add_filter( 'muplugins_loaded', 'wp_stream_manually_load_mercator' );

/**
* Manually creates EDD's database tables, users, and settings for testing.
*/
function xwp_install_edd() {
function wp_stream_install_edd() {

edd_install();

global $current_user, $edd_options;

$edd_options = get_option( 'edd_settings' );

$current_user = new WP_User(1);
$current_user->set_role('administrator');
wp_update_user( array( 'ID' => 1, 'first_name' => 'Admin', 'last_name' => 'User' ) );
$current_user = new WP_User( 1 ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$current_user->set_role( 'administrator' );
wp_update_user(
array(
'ID' => 1,
'first_name' => 'Admin',
'last_name' => 'User',
)
);
add_filter( 'edd_log_email_errors', '__return_false' );

add_filter(
'pre_http_request',
function( $status = false, $args = array(), $url = '') {
return new WP_Error( 'no_reqs_in_unit_tests', __( 'HTTP Requests disabled for unit tests', 'easy-digital-downloads' ) );
function () {
return new WP_Error( 'no_reqs_in_unit_tests', __( 'HTTP Requests disabled for unit tests', 'stream' ) );
}
);
}
Expand All @@ -94,7 +105,7 @@ function( $status = false, $args = array(), $url = '') {
define( 'WP_USE_THEMES', false );
activate_plugin( 'easy-digital-downloads/easy-digital-downloads.php' );
activate_plugin( 'wordpress-seo/wp-seo.php' );
xwp_install_edd();
wp_stream_install_edd();

require __DIR__ . '/testcase.php';

Expand Down
35 changes: 35 additions & 0 deletions tests/phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0"?>
<ruleset name="WordPress Coding Standards for WP Stream Tests">
<config name="ignore_warnings_on_exit" value="1" /><!-- FIXME -->

<arg name="extensions" value="php" />
<arg name="colors" />
<arg value="sp" /><!-- Show sniff codes in all reports and progress. -->

<rule ref="PHPCompatibilityWP" />
<config name="testVersion" value="8.1-" />

<rule ref="WordPress-Docs">
<type>warning</type><!-- FIXME Report but ignore for now. -->
</rule>

<rule ref="WordPress-Extra" />

<rule ref="WordPress-Core">
<exclude name="WordPress.Files.FileName.InvalidClassFileName" />
<exclude name="WordPress.WP.I18n.MissingTranslatorsComment" />
</rule>

<rule ref="Squiz.Commenting">
<exclude name="Squiz.Commenting.FileComment.Missing" />
<exclude name="Squiz.Commenting.ClassComment.Missing" />
<exclude name="Squiz.Commenting.FunctionComment.Missing" />
<exclude name="Squiz.Commenting.FunctionComment.MissingParamComment" />
<exclude name="Squiz.Commenting.InlineComment.InvalidEndChar" />
<exclude name="Squiz.Commenting.VariableComment.Missing" />
</rule>

<rule ref="Generic.CodeAnalysis">
<exclude name="Generic.CodeAnalysis.EmptyStatement.DetectedCatch" />
</rule>
</ruleset>
14 changes: 8 additions & 6 deletions tests/testcase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class WP_StreamTestCase extends \WP_Ajax_UnitTestCase {

/**
* Custom action prefix for test custom triggered actions
*
* @var string
*/
protected $action_prefix = 'wp_stream_test_';
Expand All @@ -27,7 +28,7 @@ class WP_StreamTestCase extends \WP_Ajax_UnitTestCase {
*
* @return void
*/
function setUp(): void {
public function setUp(): void {
parent::setUp();
$this->plugin = $GLOBALS['wp_stream'];
$this->assertNotEmpty( $this->plugin );
Expand All @@ -39,17 +40,17 @@ function setUp(): void {
* @param array $tests
* @param string $function_call
*/
protected function do_action_validation( array $tests = array(), $function_call = 'has_action' ){
protected function do_action_validation( array $tests = array(), $function_call = 'has_action' ) {
foreach ( $tests as $test ) {
list( $action, $class, $function ) = $test;

//Default WP priority
// Default WP priority
$priority = isset( $test[3] ) ? $test[3] : 10;

//Default function call
// Default function call
$function_call = ( in_array( $function_call, array( 'has_action', 'has_filter' ), true ) ) ? $function_call : 'has_action';

//Run assertion here
// Run assertion here
$this->assertEquals(
$priority,
$function_call( $action, array( $class, $function ) ),
Expand All @@ -64,9 +65,10 @@ protected function do_action_validation( array $tests = array(), $function_call

/**
* Helper function to check validity of filters
*
* @param array $tests
*/
protected function do_filter_validation( array $tests = array() ){
protected function do_filter_validation( array $tests = array() ) {
$this->do_action_validation( $tests, 'has_filter' );
}
}
10 changes: 6 additions & 4 deletions tests/tests/alerts/test-class-alert-trigger-action.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php
namespace WP_Stream;

/**
* Class Test_Alert_Trigger_Action
*
* @package WP_Stream
* @group alerts
*/
Expand All @@ -11,19 +13,19 @@ public function setUp(): void {
parent::setUp();
$this->trigger = new Alert_Trigger_Action( $this->plugin );

$this->alert = $this->plugin->alerts->get_alert();
$this->alert = $this->plugin->alerts->get_alert();
$this->alert->alert_meta['trigger_action'] = 'activated';
}

function test_check_record_bad() {
$data = $this->dummy_stream_data();
public function test_check_record_bad() {
$data = $this->dummy_stream_data();
$data['action'] = 'updated';

$status = $this->trigger->check_record( true, null, $data, $this->alert );
$this->assertFalse( $status );
}

function test_save_fields() {
public function test_save_fields() {
$_POST['wp_stream_trigger_action'] = 'updated';

$this->assertNotEquals( 'updated', $this->alert->alert_meta['trigger_action'] );
Expand Down
10 changes: 6 additions & 4 deletions tests/tests/alerts/test-class-alert-trigger-author.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php
namespace WP_Stream;

/**
* Class Test_Alert_Trigger_Author
*
* @package WP_Stream
* @group alerts
*/
Expand All @@ -11,19 +13,19 @@ public function setUp(): void {
parent::setUp();
$this->trigger = new Alert_Trigger_Author( $this->plugin );

$this->alert = $this->plugin->alerts->get_alert();
$this->alert = $this->plugin->alerts->get_alert();
$this->alert->alert_meta['trigger_author'] = '1';
}

function test_check_record_bad() {
$data = $this->dummy_stream_data();
public function test_check_record_bad() {
$data = $this->dummy_stream_data();
$data['user_id'] = '2';

$status = $this->trigger->check_record( true, null, $data, $this->alert );
$this->assertFalse( $status );
}

function test_save_fields() {
public function test_save_fields() {
$_POST['wp_stream_trigger_author'] = '0';

$this->assertNotEquals( '0', $this->alert->alert_meta['trigger_author'] );
Expand Down
16 changes: 9 additions & 7 deletions tests/tests/alerts/test-class-alert-trigger-context.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php
namespace WP_Stream;

/**
* Class Test_Alert_Trigger_Context
*
* @package WP_Stream
* @group alerts
*/
Expand All @@ -11,23 +13,23 @@ public function setUp(): void {
parent::setUp();
$this->trigger = new Alert_Trigger_Context( $this->plugin );

$this->alert = $this->plugin->alerts->get_alert();
$this->alert = $this->plugin->alerts->get_alert();
$this->alert->alert_meta['trigger_connector'] = 'installer';
$this->alert->alert_meta['trigger_context'] = 'plugins';
$this->alert->alert_meta['trigger_context'] = 'plugins';
}

function test_check_record_bad() {
$data = $this->dummy_stream_data();
public function test_check_record_bad() {
$data = $this->dummy_stream_data();
$data['connector'] = 'settings';
$data['context'] = 'general';
$data['context'] = 'general';

$status = $this->trigger->check_record( true, null, $data, $this->alert );
$this->assertFalse( $status );
}

function test_save_fields() {
public function test_save_fields() {
$_POST['wp_stream_trigger_connector'] = 'settings';
$_POST['wp_stream_trigger_context'] = 'general';
$_POST['wp_stream_trigger_context'] = 'general';

$this->assertNotEquals( 'settings', $this->alert->alert_meta['trigger_connector'] );
$this->assertNotEquals( 'general', $this->alert->alert_meta['trigger_context'] );
Expand Down
20 changes: 7 additions & 13 deletions tests/tests/connectors/test-class-connector-acf.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*
* @package WP_Stream
*/

namespace WP_Stream;

class Test_WP_Stream_Connector_ACF extends WP_StreamTestCase {
Expand Down Expand Up @@ -33,13 +34,6 @@ public function setUp(): void {
$this->mock->register();
}

/**
* Runs after each test
*/
public function tearDown(): void {
parent::tearDown();
}

/**
* Create/Update ACF field group
*
Expand Down Expand Up @@ -78,7 +72,7 @@ private function update_acf_field_group( $config = array() ) {
* @param array $config ACF field configuration.
*/
private function update_acf_field( $config = array() ) {
$defaults = [
$defaults = array(
'parent' => $this->group_key,
'key' => uniqid(),
'label' => 'Test Field',
Expand All @@ -97,7 +91,7 @@ private function update_acf_field( $config = array() ) {
'prepend' => '',
'append' => '',
'maxlength' => '',
];
);

return \acf_update_field( array_merge( $defaults, $config ) );
}
Expand Down Expand Up @@ -200,7 +194,7 @@ public function test_check_meta_values() {
// Register test ACF field group and field for later use.
$field_group = $this->update_acf_field_group(
array(
'location' => array(
'location' => array(
array(
array(
'param' => 'post_type',
Expand Down Expand Up @@ -247,7 +241,7 @@ public function test_check_meta_values() {
),
$this->equalTo( $post_id ),
$this->equalTo( 'values' ),
$this->equalTo( 'updated' )
$this->equalTo( 'updated' ),
),
array(
$this->equalTo( esc_html_x( '"%1$s" of "%2$s" %3$s updated', 'acf', 'stream' ) ),
Expand All @@ -263,7 +257,7 @@ public function test_check_meta_values() {
),
$this->equalTo( $post_id ),
$this->equalTo( 'values' ),
$this->equalTo( 'updated' )
$this->equalTo( 'updated' ),
),
array(
$this->equalTo( esc_html_x( '"%1$s" of "%2$s" %3$s updated', 'acf', 'stream' ) ),
Expand All @@ -279,7 +273,7 @@ public function test_check_meta_values() {
),
$this->equalTo( $user_id ),
$this->equalTo( 'values' ),
$this->equalTo( 'updated' )
$this->equalTo( 'updated' ),
)
);

Expand Down
Loading

0 comments on commit 26ac1ba

Please sign in to comment.