woocommerce/plugins/woocommerce-admin/tests/notes/class-wc-tests-email-notes.php

130 lines
5.0 KiB
PHP
Raw Normal View History

Add merchant email notifications (https://github.com/woocommerce/woocommerce-admin/pull/5922) * Added MerchantEmailNotifications class * Added new type and Events refactor # Conflicts: # src/Events.php * Added templates * Refactored MerchantEmailNotifications and NotificationEmail * Templates refactored * Added email opening tracking endpoint * Added templates handling * Moved folder `MerchantEmailNotifications` * Fixed template extensibility * Fixed note `heading` check * Added default type in `get_template_filename` * Added tests * Removed bypass * Modified URL * Added required noteTypes * Added flag for functionallity * Fixed plain link * Fixed comment * Turned email notifications on by default This commit adds the code to turn email notifications on by default * Fixed email styles * Fixed typo * Renamed method "possible_send" as "run" * Removed unnecessary control * Fixed another typo * Renamed method as "get_notification_email_addresses" * Refactored method "send_merchant_notification" * Renamed plain-merchant-notification * Fixed tests * Merchant email notifications - Action triggering (https://github.com/woocommerce/woocommerce-admin/pull/5976) * Added templates # Conflicts: # includes/emails/plain-mechant-notification.php # Conflicts: # includes/emails/html-merchant-notification.php * Added note action triggering This commit adds the note actions triggering # Conflicts: # includes/emails/html-merchant-notification.php * Fixed error handling * Refactored "trigger_note_action" method * Fixed actions url * Fixed note URL * Added external redirect * Added image and html handling for email * Fixed tests * Fixed buttons style Co-authored-by: Fernando Marichal <contacto@fernandomarichal.com> * Add your first product: email notification (https://github.com/woocommerce/woocommerce-admin/pull/6024) * Added AddFirstProduct note # Conflicts: # src/Events.php * Added "AddFirstProduct" email note This commit adds the email note "AddFirstProduct" * Fixed image This commit removes the image img-product-light.svg to use a png instead. Otherwise, the gmail proxy would break the image * Fixed readme.txt Co-authored-by: Fernando Marichal <contacto@fernandomarichal.com> * Added readme.txt Co-authored-by: Fernando Marichal <contacto@fernandomarichal.com>
2021-01-13 00:09:22 +00:00
<?php
/**
* Email notes tests
*
* @package WooCommerce\Admin\Tests\Notes
*/
use \Automattic\WooCommerce\Admin\Notes\Notes;
use \Automattic\WooCommerce\Admin\Notes\Note;
use \Automattic\WooCommerce\Admin\Notes\MerchantEmailNotifications\MerchantEmailNotifications;
use \Automattic\WooCommerce\Admin\Notes\MerchantEmailNotifications\NotificationEmail;
/**
* Class WC_Tests_Email_Notes
*/
class WC_Tests_Email_Notes extends WC_Unit_Test_Case {
/**
* Setup test admin notes data. Called before every test.
*
* @since 3.5.0
*/
public function setUp() {
parent::setUp();
$this->user = $this->factory->user->create(
array(
'role' => 'administrator',
)
);
WC_Helper_Admin_Notes::reset_notes_dbs();
WC_Helper_Admin_Notes::add_notes_for_tests();
WC_Helper_Admin_Notes::add_email_notes_for_test();
}
/**
* Tests NotificationEmail default values.
*/
public function test_default_values_create_notification_email() {
$note = new Note();
$note->set_title( 'PHPUNIT_TEST_NOTE_EMAIL_TITLE' );
$note->set_content( 'PHPUNIT_TEST_NOTE_EMAIL_CONTENT' );
$note->set_type( Note::E_WC_ADMIN_NOTE_EMAIL );
$note->set_name( 'PHPUNIT_TEST_NOTE_EMAIL_NAME' );
$note->set_source( 'PHPUNIT_TEST' );
$note->set_is_snoozable( false );
$note->set_layout( 'plain' );
$note->set_image( '' );
$content_data = array(
'role' => 'administrator',
);
$note->set_content_data( (object) $content_data );
$note->add_action(
'PHPUNIT_TEST_EMAIL_ACTION_SLUG',
'PHPUNIT_TEST_EMAIL_ACTION_LABEL',
'?s=PHPUNIT_TEST_EMAIL_ACTION_URL'
);
$note->set_is_deleted( false );
$notification_email = new NotificationEmail( $note );
$this->assertEquals( $notification_email->id, 'merchant_notification' );
$this->assertEquals( $notification_email->get_default_heading(), $note->get_title() );
$this->assertEquals( $notification_email->get_template_filename(), 'html-merchant-notification.php' );
$this->assertEquals( $notification_email->get_template_filename( 'html' ), 'html-merchant-notification.php' );
$this->assertEquals( $notification_email->get_template_filename( 'plain' ), 'plain-merchant-notification.php' );
}
/**
* Tests NotificationEmail is created correctly.
*/
public function test_create_notification_email() {
$data_store = WC_Data_Store::load( 'admin-note' );
$note_data = $data_store->get_notes(
array(
'type' => array( Note::E_WC_ADMIN_NOTE_EMAIL ),
'status' => array( 'unactioned' ),
)
);
$note = Notes::get_note( $note_data[0]->note_id );
$content_data = array(
'heading' => 'PHPUNIT_TEST_EMAIL_HEADING',
'role' => 'administrator',
);
$note->set_content_data( (object) $content_data );
$note->save();
$notification_email = new NotificationEmail( $note );
$notification_email->opened_tracking_url = 'PHPUNIT_TEST_NOTE_EMAIL_TRACKING_URL';
$notification_email->trigger_note_action_url = 'PHPUNIT_TEST_NOTE_EMAIL_TRIGGER_ACTION_URL';
$content_html = $notification_email->get_content_html();
$content_plain = $notification_email->get_content_plain();
$this->assertEquals( $notification_email->get_default_heading(), $content_data['heading'] );
$this->assertEquals( $notification_email->get_default_subject(), $note->get_title() );
$this->assertEquals( $notification_email->get_note_content(), $note->get_content() );
$this->assertEquals( $notification_email->get_note_content(), $note->get_content() );
$this->assertTrue( strpos( $content_html, 'PHPUNIT_TEST_NOTE_5_ACTION_URL' ) >= 0 );
$this->assertTrue( strpos( $content_html, 'PHPUNIT_TEST_NOTE_5_ACTION_LABEL' ) >= 0 );
$this->assertTrue( strpos( $content_html, 'PHPUNIT_TEST_NOTE_5_CONTENT' ) >= 0 );
$this->assertTrue( strpos( $content_html, 'PHPUNIT_TEST_NOTE_EMAIL_TRACKING_URL' ) >= 0 );
$this->assertTrue( strpos( $content_html, 'PHPUNIT_TEST_NOTE_EMAIL_TRIGGER_ACTION_URL' ) >= 0 );
$this->assertTrue( strpos( $content_plain, 'PHPUNIT_TEST_NOTE_5_ACTION_URL' ) >= 0 );
$this->assertTrue( strpos( $content_plain, 'PHPUNIT_TEST_NOTE_5_ACTION_LABEL' ) >= 0 );
$this->assertTrue( strpos( $content_plain, 'PHPUNIT_TEST_NOTE_5_CONTENT' ) >= 0 );
$this->assertTrue( strpos( $content_plain, 'PHPUNIT_TEST_EMAIL_HEADING' ) >= 0 );
}
/**
* Tests NotificationEmail validations.
*/
public function test_create_invalid_notification_email() {
$data_store = WC_Data_Store::load( 'admin-note' );
$note_data = $data_store->get_notes(
array(
'type' => array( Note::E_WC_ADMIN_NOTE_EMAIL ),
'status' => array( 'unactioned' ),
)
);
$note = Notes::get_note( $note_data[0]->note_id );
$content_data = array(
'role' => 'invalid_role',
);
$note->set_content_data( (object) $content_data );
$notification_email = new NotificationEmail( $note );
$this->assertEmpty( MerchantEmailNotifications::get_notification_email_addresses( $note ) );
$this->assertEmpty( $notification_email->get_template_filename( 'wrong_type' ) );
}
}