Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP alert type tests implemented #1196

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
highlight, ifttt, none, and slack alert type tests added.
kidunot89 committed Oct 31, 2020
commit c41e2868f0396601ba5846f4060c0673f0747cd3
1 change: 1 addition & 0 deletions alerts/class-alert-type-ifttt.php
Original file line number Diff line number Diff line change
@@ -99,6 +99,7 @@ public function __construct( $plugin ) {
*/
public function alert( $record_id, $recordarr, $alert ) {
$recordarr['ID'] = $record_id;
error_log( 'here' );
$this->notify_ifttt( $alert, $recordarr );
}

1 change: 1 addition & 0 deletions classes/class-alert.php
Original file line number Diff line number Diff line change
@@ -282,6 +282,7 @@ public static function update_record_triggered_alerts( $record, $alert_slug, $al
} elseif ( ! array_key_exists( $alert_slug, $alerts_triggered ) || ! is_array( $alerts_triggered[ $alert_slug ] ) ) {
$alerts_triggered[ $alert_slug ] = $alert_meta;
}

return $record->update_meta( Alerts::ALERTS_TRIGGERED_META_KEY, $alerts_triggered );
}

36 changes: 0 additions & 36 deletions tests/tests/alerts/test-class-alert-type-die.php

This file was deleted.

3 changes: 1 addition & 2 deletions tests/tests/alerts/test-class-alert-type-email.php
Original file line number Diff line number Diff line change
@@ -8,7 +8,6 @@
namespace WP_Stream;

class Test_Alert_Type_Email extends WP_StreamTestCase {

/**
* Runs before each test
*/
@@ -18,7 +17,7 @@ public function setUp() {
$post_connector = new Connector_Posts();
$post_connector->register();

$alert_type = new Alert_Type_Email( $this->plugin );
$this->alerts = new Alerts( $this->plugin );
}

public function test_alert() {
75 changes: 75 additions & 0 deletions tests/tests/alerts/test-class-alert-type-highlight.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/**
* Tests for "highlight" alert type.
*
* @package WP_Stream
*/

namespace WP_Stream;

class Test_Alert_Type_Highlight extends WP_StreamTestCase {

/**
* Runs before each test
*/
public function setUp() {
parent::setUp();

$post_connector = new Connector_Posts();
$post_connector->register();

$this->alert_type = new Alert_Type_Highlight( $this->plugin );
}

public function test_alert() {

// Set alert fields
try {
$_POST['wp_stream_alerts_nonce'] = wp_create_nonce( 'save_alert' );
$_POST['wp_stream_trigger_author'] = 1;
$_POST['wp_stream_trigger_context'] = 'posts-post';
$_POST['wp_stream_trigger_action'] = 'created';
$_POST['wp_stream_alert_type'] = 'highlight';
$_POST['wp_stream_alert_status'] = 'wp_stream_enabled';

// Highlight alert meta.
$_POST['wp_stream_highlight_color'] = 'yellow';

// Simulate saving an alert.
$this->_handleAjax( 'save_new_alert' );
} catch ( \WPAjaxDieContinueException $e ) {
$exception = $e;
}

$response = json_decode( $this->_last_response );
$this->assertInternalType( 'object', $response );
$this->assertObjectHasAttribute( 'success', $response );
$this->assertTrue( $response->success );

// Trigger alert.
wp_set_current_user( 1 );
$post_id = wp_insert_post(
array(
'post_title' => 'Test post',
'post_content' => 'Lorem ipsum dolor...',
'post_status' => 'publish',
'post_author' => 1
)
);

// Retrieve record.
$record = ( $this->plugin->db->query( array( 'object_id' => $post_id ) ) )[0];
$record = new Record( $record );

// Retrieve alert trigger meta.
$record->meta[ Alerts::ALERTS_TRIGGERED_META_KEY ] = $record->get_meta( Alerts::ALERTS_TRIGGERED_META_KEY, true );

$this->assertTrue(
in_array(
'alert-highlight highlight-yellow record-id-' . $record->ID,
$this->alert_type->post_class( array(), $record ),
true
)
);
}
}
76 changes: 76 additions & 0 deletions tests/tests/alerts/test-class-alert-type-ifttt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* Tests for "IFTTT" alert type.
*
* @package WP_Stream
*/

namespace WP_Stream;

class Test_Alert_Type_IFTTT extends WP_StreamTestCase {
/**
* Runs before each test
*/
public function setUp() {
parent::setUp();

$post_connector = new Connector_Posts();
$post_connector->register();

$this->alerts = new Alerts( $this->plugin );
}

public function test_alert() {
// Set alert fields
try {
$_POST['wp_stream_alerts_nonce'] = wp_create_nonce( 'save_alert' );
$_POST['wp_stream_trigger_author'] = 1;
$_POST['wp_stream_trigger_context'] = 'posts-post';
$_POST['wp_stream_trigger_action'] = 'created';
$_POST['wp_stream_alert_type'] = 'ifttt';
$_POST['wp_stream_alert_status'] = 'wp_stream_enabled';

// IFTTT alert meta.
$_POST['wp_stream_ifttt_maker_key'] = 'http://example.com';
$_POST['wp_stream_ifttt_event_name'] = 'Post created';

// Simulate saving an alert.
$this->_handleAjax( 'save_new_alert' );
} catch ( \WPAjaxDieContinueException $e ) {
$exception = $e;
}

$response = json_decode( $this->_last_response );
$this->assertInternalType( 'object', $response );
$this->assertObjectHasAttribute( 'success', $response );
$this->assertTrue( $response->success );

// Use filter callback to the 'wp_mail' as a place to run assertions.
$asserted = false;
add_filter(
'http_request_args',
function( $parsed_args ) use ( &$asserted ) {
$this->assertNotEmpty( $parsed_args['body'] );
$body = wp_json_decode( $parsed_args['body'] );

error_log( print_r( $body, true ) );
$this->assertNotEmpty( $body );
$asserted = true;
}
);

// Trigger alert.
wp_set_current_user( 1 );
$post_id = wp_insert_post(
array(
'post_title' => 'Test post',
'post_content' => 'Lorem ipsum dolor...',
'post_status' => 'publish',
'post_author' => 1
)
);

// Confirm assertion were run.
$this->assertTrue( $asserted );
}
}
57 changes: 57 additions & 0 deletions tests/tests/alerts/test-class-alert-type-none.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Tests for "none" alert type.
*
* @package WP_Stream
*/

namespace WP_Stream;

class Test_Alert_Type_None extends WP_StreamTestCase {
/**
* Runs before each test
*/
public function setUp() {
parent::setUp();

$post_connector = new Connector_Posts();
$post_connector->register();

$this->alerts = new Alerts( $this->plugin );
}

public function test_alert() {
// Set alert fields
try {
$_POST['wp_stream_alerts_nonce'] = wp_create_nonce( 'save_alert' );
$_POST['wp_stream_trigger_author'] = 1;
$_POST['wp_stream_trigger_context'] = 'posts-post';
$_POST['wp_stream_trigger_action'] = 'created';
$_POST['wp_stream_alert_type'] = 'none';
$_POST['wp_stream_alert_status'] = 'wp_stream_enabled';

// None alert meta.

// Simulate saving an alert.
$this->_handleAjax( 'save_new_alert' );
} catch ( \WPAjaxDieContinueException $e ) {
$exception = $e;
}

$response = json_decode( $this->_last_response );
$this->assertInternalType( 'object', $response );
$this->assertObjectHasAttribute( 'success', $response );
$this->assertTrue( $response->success );

// Trigger alert.
wp_set_current_user( 1 );
$post_id = wp_insert_post(
array(
'post_title' => 'Test post',
'post_content' => 'Lorem ipsum dolor...',
'post_status' => 'publish',
'post_author' => 1
)
);
}
}
78 changes: 78 additions & 0 deletions tests/tests/alerts/test-class-alert-type-slack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/**
* Tests for "Slack" alert type.
*
* @package WP_Stream
*/

namespace WP_Stream;

class Test_Alert_Type_Slack extends WP_StreamTestCase {
/**
* Runs before each test
*/
public function setUp() {
parent::setUp();

$post_connector = new Connector_Posts();
$post_connector->register();

$this->alerts = new Alerts( $this->plugin );
}

public function test_alert() {
// Set alert fields
try {
$_POST['wp_stream_alerts_nonce'] = wp_create_nonce( 'save_alert' );
$_POST['wp_stream_trigger_author'] = 1;
$_POST['wp_stream_trigger_context'] = 'posts-post';
$_POST['wp_stream_trigger_action'] = 'created';
$_POST['wp_stream_alert_type'] = 'none';
$_POST['wp_stream_alert_status'] = 'wp_stream_enabled';

// Slack alert meta.
$_POST['wp_stream_slack_webhook'] = '';
$_POST['wp_stream_slack_channel'] = '';
$_POST['wp_stream_slack_username'] = '';
$_POST['wp_stream_slack_icon'] = '';

// Simulate saving an alert.
$this->_handleAjax( 'save_new_alert' );
} catch ( \WPAjaxDieContinueException $e ) {
$exception = $e;
}

$response = json_decode( $this->_last_response );
$this->assertInternalType( 'object', $response );
$this->assertObjectHasAttribute( 'success', $response );
$this->assertTrue( $response->success );

// Use filter callback to the 'wp_mail' as a place to run assertions.
$asserted = false;
add_filter(
'http_request_args',
function( $parsed_args ) use ( &$asserted ) {
$this->assertNotEmpty( $parsed_args['body'] );
$body = wp_json_decode( $parsed_args['body'] );

error_log( print_r( $body, true ) );
$this->assertNotEmpty( $body );
$asserted = true;
}
);

// Trigger alert.
wp_set_current_user( 1 );
$post_id = wp_insert_post(
array(
'post_title' => 'Test post',
'post_content' => 'Lorem ipsum dolor...',
'post_status' => 'publish',
'post_author' => 1
)
);

// Confirm assertion were run.
$this->assertTrue( $asserted );
}
}