Add unit tests for orders milestone note (#34295)
* Refactor OrderMilestones.php * Add unit tests for Orders Milestones note * Add changelog * Update OrderMilestones.php * Fix OrderMilestones.php
This commit is contained in:
parent
71ffd2c548
commit
ed9cd231d2
|
@ -0,0 +1,4 @@
|
|||
Significance: patch
|
||||
Type: add
|
||||
|
||||
Refactor and add unit tests for "Orders Milestones" note
|
|
@ -83,7 +83,7 @@ class Events {
|
|||
/**
|
||||
* Daily events to run.
|
||||
*
|
||||
* Note: Order_Milestones::other_milestones is hooked to this as well.
|
||||
* Note: Order_Milestones::possibly_add_note is hooked to this as well.
|
||||
*/
|
||||
public function do_wc_admin_daily() {
|
||||
$this->possibly_add_notes();
|
||||
|
|
|
@ -96,7 +96,7 @@ class OrderMilestones {
|
|||
|
||||
add_action( 'wc_admin_installed', array( $this, 'backfill_last_milestone' ) );
|
||||
|
||||
add_action( self::PROCESS_ORDERS_MILESTONE_HOOK, array( $this, 'other_milestones' ) );
|
||||
add_action( self::PROCESS_ORDERS_MILESTONE_HOOK, array( $this, 'possibly_add_note' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -289,35 +289,58 @@ class OrderMilestones {
|
|||
}
|
||||
|
||||
/**
|
||||
* Add milestone notes for other significant thresholds.
|
||||
* Get the note by milestones.
|
||||
*
|
||||
* @param int $current_milestone Current milestone.
|
||||
*
|
||||
* @return Note
|
||||
*/
|
||||
public function other_milestones() {
|
||||
public function get_note_by_milestone( $current_milestone ) {
|
||||
$note = new Note();
|
||||
$note->set_title( self::get_note_title_for_milestone( $current_milestone ) );
|
||||
$note->set_content( self::get_note_content_for_milestone( $current_milestone ) );
|
||||
$note_action = self::get_note_action_for_milestone( $current_milestone );
|
||||
$note->add_action( $note_action['name'], $note_action['label'], $note_action['query'] );
|
||||
$note->set_type( Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
|
||||
$note->set_name( self::NOTE_NAME );
|
||||
$note->set_source( 'woocommerce-admin' );
|
||||
return $note;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a note can and should be added.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function can_be_added() {
|
||||
// If the milestone notes have been disabled via filter, bail.
|
||||
if ( ! $this->are_milestones_enabled() ) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
$last_milestone = $this->get_last_milestone();
|
||||
$current_milestone = $this->get_current_milestone();
|
||||
|
||||
if ( $current_milestone <= $last_milestone ) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add milestone notes for other significant thresholds.
|
||||
*/
|
||||
public function possibly_add_note() {
|
||||
if ( ! self::can_be_added() ) {
|
||||
return;
|
||||
}
|
||||
$current_milestone = $this->get_current_milestone();
|
||||
$this->set_last_milestone( $current_milestone );
|
||||
|
||||
// We only want one milestone note at any time.
|
||||
Notes::delete_notes_with_name( self::NOTE_NAME );
|
||||
|
||||
// Add the milestone note.
|
||||
$note = new Note();
|
||||
$note->set_title( $this->get_note_title_for_milestone( $current_milestone ) );
|
||||
$note->set_content( $this->get_note_content_for_milestone( $current_milestone ) );
|
||||
$note_action = $this->get_note_action_for_milestone( $current_milestone );
|
||||
$note->add_action( $note_action['name'], $note_action['label'], $note_action['query'] );
|
||||
$note->set_type( Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
|
||||
$note->set_name( self::NOTE_NAME );
|
||||
$note->set_source( 'woocommerce-admin' );
|
||||
$note = $this->get_note_by_milestone( $current_milestone );
|
||||
$note->save();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,116 @@
|
|||
<?php
|
||||
/**
|
||||
* OrderMilestones note tests
|
||||
*
|
||||
* @package WooCommerce\Admin\Tests\Notes
|
||||
*/
|
||||
|
||||
use \Automattic\WooCommerce\Internal\Admin\Notes\OrderMilestones;
|
||||
use Automattic\WooCommerce\Admin\Notes\Notes;
|
||||
|
||||
/**
|
||||
* Class WC_Admin_Tests_Marketing_Notes
|
||||
*/
|
||||
class WC_Admin_Tests_Order_Milestones extends WC_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* @var OrderMilestones
|
||||
*/
|
||||
private $instance;
|
||||
|
||||
/**
|
||||
* setUp
|
||||
*/
|
||||
public function setUp(): void {
|
||||
parent::setUp();
|
||||
$this->instance = new OrderMilestones();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests can_be_added method return false when woocommerce_admin_order_milestones_enabled is false.
|
||||
*/
|
||||
public function test_can_be_added_when_milestones_disabled() {
|
||||
add_filter(
|
||||
'woocommerce_admin_order_milestones_enabled',
|
||||
function ( $enabled ) {
|
||||
return false;
|
||||
}
|
||||
);
|
||||
$this->assertFalse( $this->instance->can_be_added() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests can_be_added method return false when no orders received.
|
||||
*/
|
||||
public function test_can_be_added_when_no_orders_received() {
|
||||
$this->assertFalse( $this->instance->can_be_added() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests can_be_added method return true when one orders received.
|
||||
*/
|
||||
public function test_can_be_added_when_orders_received() {
|
||||
WC_Helper_Order::create_order();
|
||||
$this->assertTrue( $this->instance->can_be_added() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests get_note_by_milestone method when milestone is 1.
|
||||
*/
|
||||
public function test_get_note_by_milestone_when_milestone_is_1() {
|
||||
$note = $this->instance->get_note_by_milestone( 1 );
|
||||
$this->assertEquals( $note->get_title(), 'First order received' );
|
||||
$this->assertEquals( $note->get_content(), 'Congratulations on getting your first order! Now is a great time to learn how to manage your orders.' );
|
||||
$this->assertEquals( $note->get_actions()[0]->label, 'Learn more' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests get_note_by_milestone method when no milestone is 10.
|
||||
*/
|
||||
public function test_get_note_by_milestone_when_milestone_is_10() {
|
||||
$note = $this->instance->get_note_by_milestone( 10 );
|
||||
$this->assertEquals( $note->get_title(), 'Congratulations on processing 10 orders!' );
|
||||
$this->assertEquals( $note->get_content(), "You've hit the 10 orders milestone! Look at you go. Browse some WooCommerce success stories for inspiration." );
|
||||
$this->assertEquals( $note->get_actions()[0]->label, 'Browse' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests get_note_by_milestone method when no milestone is 100.
|
||||
*/
|
||||
public function test_get_note_by_milestone_when_milestone_is_100() {
|
||||
$note = $this->instance->get_note_by_milestone( 100 );
|
||||
$this->assertEquals( $note->get_title(), 'Congratulations on processing 100 orders!' );
|
||||
$this->assertEquals( $note->get_content(), 'Another order milestone! Take a look at your Orders Report to review your orders to date.' );
|
||||
$this->assertEquals( $note->get_actions()[0]->label, 'Review your orders' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests possibly_add_note method when no orders received.
|
||||
*/
|
||||
public function test_possibly_add_note_when_no_orders_received() {
|
||||
$this->instance->possibly_add_note();
|
||||
$this->assertFalse( Notes::get_note_by_name( OrderMilestones::NOTE_NAME ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests possibly_add_note method when no orders received.
|
||||
*/
|
||||
public function test_possibly_add_note_when_first_order_received() {
|
||||
WC_Helper_Order::create_order();
|
||||
$this->instance->possibly_add_note();
|
||||
$note = Notes::get_note_by_name( OrderMilestones::NOTE_NAME );
|
||||
$this->assertEquals( $note->get_title(), 'First order received' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests possibly_add_note method when tenth order received.
|
||||
*/
|
||||
public function test_possibly_add_note_when_tenth_order_received() {
|
||||
for ( $i = 0; $i < 10; $i++ ) {
|
||||
WC_Helper_Order::create_order();
|
||||
}
|
||||
$this->instance->possibly_add_note();
|
||||
$note = Notes::get_note_by_name( OrderMilestones::NOTE_NAME );
|
||||
$this->assertEquals( $note->get_content(), "You've hit the 10 orders milestone! Look at you go. Browse some WooCommerce success stories for inspiration." );
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue