Skip to content

Commit 99a6390

Browse files
committed
chore: added unit tests for welcome module
1 parent 0540a2a commit 99a6390

File tree

1 file changed

+193
-0
lines changed

1 file changed

+193
-0
lines changed

tests/welcome-test.php

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
<?php
2+
/**
3+
* Welcome module feature test.
4+
*
5+
* @package ThemeIsleSDK
6+
*/
7+
8+
/**
9+
* Test notification feature.
10+
*/
11+
class Welcome_Test extends WP_UnitTestCase {
12+
13+
protected static $editor_id;
14+
protected static $admin_id;
15+
16+
public static function wpSetUpBeforeClass( $factory ) {
17+
self::$editor_id = $factory->user->create(
18+
array(
19+
'role' => 'editor',
20+
)
21+
);
22+
self::$admin_id = $factory->user->create(
23+
array(
24+
'role' => 'administrator',
25+
)
26+
);
27+
28+
wp_set_current_user( self::$editor_id );
29+
}
30+
31+
public static function wpTearDownAfterClass() {
32+
self::delete_user( self::$editor_id );
33+
self::delete_user( self::$admin_id );
34+
}
35+
36+
public function test_product_partner_module_loading() {
37+
38+
$file = dirname( __FILE__ ) . '/sample_products/sample_theme_external/style.css';
39+
40+
\ThemeisleSDK\Loader::add_product( $file );
41+
42+
$modules = \ThemeisleSDK\Common\Module_Factory::get_modules_map();
43+
44+
$this->assertArrayHasKey( 'sample_theme_external', $modules );
45+
$modules['sample_theme_external'] = array_filter(
46+
$modules['sample_theme_external'],
47+
[ $this, 'filter_value' ]
48+
);
49+
$this->assertCount( 0, $modules['sample_theme_external'] );
50+
51+
}
52+
53+
private function filter_value( $value ) {
54+
return ( get_class( $value ) === 'ThemeisleSDK\\Modules\\Welcome' );
55+
}
56+
57+
private function add_filter( $slug, $enabled = true ) {
58+
add_filter( $slug .'_welcome_metadata', function () use ( $enabled ) {
59+
return [
60+
'is_enabled' => $enabled,
61+
'cta_link' => tsdk_utmify( 'https://link_to_upgrade.with/?discount=discunt30', 'test', 'unit_test' ),
62+
];
63+
} );
64+
}
65+
66+
private function cycle_module_load( $products_array ) {
67+
foreach ( $products_array as $product ) {
68+
$module = new \ThemeisleSDK\Modules\Welcome();
69+
$module->can_load( $product );
70+
$module->load( $product );
71+
}
72+
}
73+
74+
public function test_welcome_product_loading() {
75+
76+
$file = dirname( __FILE__ ) . '/sample_products/sample_theme/style.css';
77+
78+
\ThemeisleSDK\Loader::add_product( $file );
79+
80+
$modules = \ThemeisleSDK\Common\Module_Factory::get_modules_map();
81+
82+
$this->assertArrayHasKey( 'sample_theme', $modules );
83+
$this->assertGreaterThan( 0, count( $modules['sample_theme'] ) );
84+
85+
}
86+
87+
public function test_welcome_not_load_for_if_meta_is_disabled() {
88+
wp_set_current_user( self::$admin_id );
89+
$file = dirname( __FILE__ ) . '/sample_products/sample_theme/style.css';
90+
91+
$product = new \ThemeisleSDK\Product( $file );
92+
93+
$this->add_filter( $product->get_key(), false );
94+
95+
$this->assertFalse( ( new \ThemeisleSDK\Modules\Welcome() )->can_load( $product ) );
96+
97+
}
98+
99+
public function test_welcome_not_load_if_new() {
100+
wp_set_current_user( self::$admin_id );
101+
$file = dirname( __FILE__ ) . '/sample_products/sample_theme/style.css';
102+
103+
$product = new \ThemeisleSDK\Product( $file );
104+
105+
$this->add_filter( $product->get_key(), true );
106+
107+
$this->assertTrue( ( new \ThemeisleSDK\Modules\Welcome() )->can_load( $product ) );
108+
$this->assertTrue( is_null( ( new \ThemeisleSDK\Modules\Welcome() )->load( $product ) ) );
109+
}
110+
111+
public function test_welcome_load_after_seven_days() {
112+
wp_set_current_user( self::$admin_id );
113+
114+
$file = dirname( __FILE__ ) . '/sample_products/sample_theme/style.css';
115+
116+
update_option( 'sample_theme_install', ( time() - DAY_IN_SECONDS * 7 ) );
117+
$product = new \ThemeisleSDK\Product( $file );
118+
119+
$this->add_filter( $product->get_key(), true );
120+
121+
$this->assertTrue( ( new \ThemeisleSDK\Modules\Welcome() )->can_load( $product ) );
122+
$this->assertInstanceOf( 'ThemeisleSDK\\Modules\\Welcome', ( new \ThemeisleSDK\Modules\Welcome() )->load( $product ) );
123+
}
124+
125+
public function test_welcome_not_load_after_twelve_days() {
126+
wp_set_current_user( self::$admin_id );
127+
128+
$file = dirname( __FILE__ ) . '/sample_products/sample_theme/style.css';
129+
130+
update_option( 'sample_theme_install', ( time() - DAY_IN_SECONDS * 13 ) );
131+
$product = new \ThemeisleSDK\Product( $file );
132+
133+
$this->add_filter( $product->get_key(), true );
134+
135+
$this->assertTrue( ( new \ThemeisleSDK\Modules\Welcome() )->can_load( $product ) );
136+
$this->assertTrue( is_null( ( new \ThemeisleSDK\Modules\Welcome() )->load( $product ) ) );
137+
}
138+
139+
public function test_welcome_not_load_before_seven_days() {
140+
wp_set_current_user( self::$admin_id );
141+
142+
$file = dirname( __FILE__ ) . '/sample_products/sample_theme/style.css';
143+
144+
update_option( 'sample_theme_install', ( time() - DAY_IN_SECONDS * 6 ) );
145+
$product = new \ThemeisleSDK\Product( $file );
146+
147+
$this->add_filter( $product->get_key(), true );
148+
149+
$this->assertTrue( ( new \ThemeisleSDK\Modules\Welcome() )->can_load( $product ) );
150+
$this->assertTrue( is_null( ( new \ThemeisleSDK\Modules\Welcome() )->load( $product ) ) );
151+
}
152+
153+
public function test_welcome_random_notification() {
154+
wp_set_current_user( self::$admin_id );
155+
156+
$theme = dirname( __FILE__ ) . '/sample_products/sample_theme/style.css';
157+
$plugin = dirname( __FILE__ ) . '/sample_products/sample_plugin/plugin_file.php';
158+
159+
update_option( 'sample_theme_install', ( time() - DAY_IN_SECONDS * 7 ) );
160+
$theme_product = new \ThemeisleSDK\Product( $theme );
161+
$this->add_filter( $theme_product->get_key(), true );
162+
163+
$this->assertTrue( ( new \ThemeisleSDK\Modules\Welcome() )->can_load( $theme_product ) );
164+
$this->assertInstanceOf( 'ThemeisleSDK\\Modules\\Welcome', ( new \ThemeisleSDK\Modules\Welcome() )->load( $theme_product ) );
165+
166+
update_option( 'sample_plugin_install', ( time() - DAY_IN_SECONDS * 7 ) );
167+
$plugin_product = new \ThemeisleSDK\Product( $plugin );
168+
$this->add_filter( $plugin_product->get_key(), true );
169+
170+
$this->assertTrue( ( new \ThemeisleSDK\Modules\Welcome() )->can_load( $plugin_product ) );
171+
$this->assertInstanceOf( 'ThemeisleSDK\\Modules\\Welcome', ( new \ThemeisleSDK\Modules\Welcome() )->load( $plugin_product ) );
172+
173+
174+
$this->cycle_module_load( [ $theme_product, $plugin_product ] );
175+
176+
$registered_notifications = apply_filters( 'themeisle_sdk_registered_notifications', [] );
177+
$this->assertTrue( ! empty( $registered_notifications ) );
178+
179+
$displayed_notifications = [];
180+
for ( $i = 0; $i < 10; $i++ ) {
181+
remove_all_filters( 'themeisle_sdk_registered_notifications' );
182+
$this->cycle_module_load( [ $theme_product, $plugin_product ] );
183+
184+
$registered_notifications = apply_filters( 'themeisle_sdk_registered_notifications', [] );
185+
$id = $registered_notifications[0]['id'];
186+
$displayed_notifications[ $id ] = true;
187+
}
188+
189+
$this->assertTrue( count( $displayed_notifications ) === 2 );
190+
$this->assertTrue( array_key_exists('sample_plugin_welcome_upsell_flag', $displayed_notifications ) );
191+
$this->assertTrue( array_key_exists('sample_theme_welcome_upsell_flag', $displayed_notifications ) );
192+
}
193+
}

0 commit comments

Comments
 (0)