AdminNotesHelper::add_notes_for_tests()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 47
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 39
nc 1
nop 0
dl 0
loc 47
rs 9.296
c 0
b 0
f 0
1
<?php
2
/**
3
 * Admin notes helper for wc-admin unit tests.
4
 *
5
 * @package WooCommerce\Tests\Framework\Helpers
6
 */
7
8
namespace Automattic\WooCommerce\RestApi\UnitTests\Helpers;
9
10
defined( 'ABSPATH' ) || exit;
11
12
use \WC_Data_Store;
13
use \WC_Admin_Note;
0 ignored issues
show
Bug introduced by
The type WC_Admin_Note was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
15
/**
16
 * Class AdminNotesHelper.
17
 *
18
 * This helper class should ONLY be used for unit tests!.
19
 */
20
class AdminNotesHelper {
21
22
	/**
23
	 * Delete everything in the notes tables.
24
	 */
25
	public static function reset_notes_dbs() {
26
		global $wpdb;
27
		$wpdb->query( "TRUNCATE TABLE {$wpdb->prefix}wc_admin_notes" ); // @codingStandardsIgnoreLine.
28
		$wpdb->query( "TRUNCATE TABLE {$wpdb->prefix}wc_admin_note_actions" ); // @codingStandardsIgnoreLine.
29
	}
30
31
	/**
32
	 * Create two notes that we can use for notes REST API tests
33
	 */
34
	public static function add_notes_for_tests() {
35
		$data_store = WC_Data_Store::load( 'admin-note' );
36
37
		$note_1 = new WC_Admin_Note();
38
		$note_1->set_title( 'PHPUNIT_TEST_NOTE_1_TITLE' );
39
		$note_1->set_content( 'PHPUNIT_TEST_NOTE_1_CONTENT' );
40
		$note_1->set_content_data( (object) array( 'amount' => 1.23 ) );
41
		$note_1->set_type( WC_Admin_Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
42
		$note_1->set_icon( 'info' );
43
		$note_1->set_name( 'PHPUNIT_TEST_NOTE_NAME' );
44
		$note_1->set_source( 'PHPUNIT_TEST' );
45
		$note_1->add_action(
46
			'PHPUNIT_TEST_NOTE_1_ACTION_1_SLUG',
47
			'PHPUNIT_TEST_NOTE_1_ACTION_1_LABEL',
48
			'?s=PHPUNIT_TEST_NOTE_1_ACTION_1_URL'
49
		);
50
		$note_1->add_action(
51
			'PHPUNIT_TEST_NOTE_1_ACTION_2_SLUG',
52
			'PHPUNIT_TEST_NOTE_1_ACTION_2_LABEL',
53
			'?s=PHPUNIT_TEST_NOTE_1_ACTION_2_URL'
54
		);
55
		$note_1->save();
56
57
		$note_2 = new WC_Admin_Note();
58
		$note_2->set_title( 'PHPUNIT_TEST_NOTE_2_TITLE' );
59
		$note_2->set_content( 'PHPUNIT_TEST_NOTE_2_CONTENT' );
60
		$note_2->set_content_data( (object) array( 'amount' => 4.56 ) );
61
		$note_2->set_type( WC_Admin_Note::E_WC_ADMIN_NOTE_WARNING );
62
		$note_2->set_icon( 'info' );
63
		$note_2->set_name( 'PHPUNIT_TEST_NOTE_NAME' );
64
		$note_2->set_source( 'PHPUNIT_TEST' );
65
		$note_2->set_status( WC_Admin_Note::E_WC_ADMIN_NOTE_ACTIONED );
66
		// This note has no actions.
67
		$note_2->save();
68
69
		$note_3 = new WC_Admin_Note();
70
		$note_3->set_title( 'PHPUNIT_TEST_NOTE_3_TITLE' );
71
		$note_3->set_content( 'PHPUNIT_TEST_NOTE_3_CONTENT' );
72
		$note_3->set_content_data( (object) array( 'amount' => 7.89 ) );
73
		$note_3->set_type( WC_Admin_Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
74
		$note_3->set_icon( 'info' );
75
		$note_3->set_name( 'PHPUNIT_TEST_NOTE_NAME' );
76
		$note_3->set_source( 'PHPUNIT_TEST' );
77
		$note_3->set_status( WC_Admin_Note::E_WC_ADMIN_NOTE_SNOOZED );
78
		$note_3->set_date_reminder( time() - HOUR_IN_SECONDS );
79
		// This note has no actions.
80
		$note_3->save();
81
82
	}
83
}
84