Add configurable event webhook notifications
This commit is contained in:
1416
includes/sp-webhooks.php
Normal file
1416
includes/sp-webhooks.php
Normal file
File diff suppressed because it is too large
Load Diff
81
tests/test-sp-webhooks.php
Normal file
81
tests/test-sp-webhooks.php
Normal file
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for configurable SportsPress webhooks.
|
||||
*
|
||||
* @package Tonys_Sportspress_Enhancements
|
||||
*/
|
||||
|
||||
/**
|
||||
* Webhook feature tests.
|
||||
*/
|
||||
class Test_SP_Webhooks extends WP_UnitTestCase {
|
||||
|
||||
/**
|
||||
* Template placeholders should resolve nested values and JSON serialization.
|
||||
*/
|
||||
public function test_render_template_supports_dot_paths_and_tojson() {
|
||||
$webhooks = Tony_Sportspress_Webhooks::instance();
|
||||
$template = 'Trigger={{ trigger.key }} Team={{ event.teams.0.name }} Payload={{ event|tojson }}';
|
||||
$context = array(
|
||||
'trigger' => array(
|
||||
'key' => 'event_results_updated',
|
||||
),
|
||||
'event' => array(
|
||||
'id' => 55,
|
||||
'teams' => array(
|
||||
array(
|
||||
'name' => 'Blue Team',
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
$rendered = $webhooks->render_template( $template, $context );
|
||||
|
||||
$this->assertStringContainsString( 'Trigger=event_results_updated', $rendered );
|
||||
$this->assertStringContainsString( 'Team=Blue Team', $rendered );
|
||||
$this->assertStringContainsString( '"id":55', $rendered );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitization should keep only complete provider-specific webhook rows.
|
||||
*/
|
||||
public function test_sanitize_settings_keeps_only_valid_webhooks() {
|
||||
$webhooks = Tony_Sportspress_Webhooks::instance();
|
||||
$sanitized = $webhooks->sanitize_settings(
|
||||
array(
|
||||
'webhooks' => array(
|
||||
array(
|
||||
'name' => 'Results',
|
||||
'enabled' => '1',
|
||||
'provider' => 'google_chat',
|
||||
'url' => 'https://chat.googleapis.com/v1/spaces/AAA/messages?key=test&token=test',
|
||||
'triggers' => array( 'event_results_updated' ),
|
||||
'template' => '{"summary":"{{ results.summary }}"}',
|
||||
),
|
||||
array(
|
||||
'name' => 'Invalid',
|
||||
'enabled' => '1',
|
||||
'provider' => 'groupme_bot',
|
||||
'url' => 'invalid bot id',
|
||||
'triggers' => array( 'event_datetime_changed' ),
|
||||
'template' => 'ignored',
|
||||
),
|
||||
array(
|
||||
'name' => 'Missing trigger',
|
||||
'enabled' => '1',
|
||||
'provider' => 'generic_json',
|
||||
'url' => 'https://example.com/missing-trigger',
|
||||
'template' => 'ignored',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
$this->assertCount( 1, $sanitized['webhooks'] );
|
||||
$this->assertSame( 'Results', $sanitized['webhooks'][0]['name'] );
|
||||
$this->assertSame( 'google_chat', $sanitized['webhooks'][0]['provider'] );
|
||||
$this->assertSame( 'https://chat.googleapis.com/v1/spaces/AAA/messages?key=test&token=test', $sanitized['webhooks'][0]['url'] );
|
||||
$this->assertSame( array( 'event_results_updated' ), $sanitized['webhooks'][0]['triggers'] );
|
||||
}
|
||||
}
|
||||
@@ -46,6 +46,7 @@ require_once plugin_dir_path(__FILE__) . 'includes/sp-event-quick-edit-officials
|
||||
require_once plugin_dir_path(__FILE__) . 'includes/sp-event-team-ordering.php';
|
||||
require_once plugin_dir_path(__FILE__) . 'includes/sp-printable-calendars.php';
|
||||
require_once plugin_dir_path(__FILE__) . 'includes/sp-url-builder.php';
|
||||
require_once plugin_dir_path(__FILE__) . 'includes/sp-webhooks.php';
|
||||
require_once plugin_dir_path(__FILE__) . 'includes/sp-schedule-exporter.php';
|
||||
require_once plugin_dir_path(__FILE__) . 'includes/sp-venue-meta.php';
|
||||
|
||||
|
||||
Reference in New Issue
Block a user