Skip to content

Commit 29d875c

Browse files
committed
add tests and phan baseline to allow deprecated function call
1 parent d8596ce commit 29d875c

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed

projects/packages/forms/.phan/baseline.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
'src/dashboard/class-dashboard-view-switch.php' => ['PhanUnreferencedUseNormal'],
3333
'src/service/class-google-drive.php' => ['PhanTypeMismatchReturnProbablyReal'],
3434
'tests/php/contact-form/Contact_Form_Plugin_Test.php' => ['PhanPluginMixedKeyNoKey'],
35+
'tests/php/contact-form/Util_Test.php' => ['PhanDeprecatedFunction'],
3536
],
3637
// 'directory_suppressions' => ['src/directory_name' => ['PhanIssueName1', 'PhanIssueName2']] can be manually added if needed.
3738
// (directory_suppressions will currently be ignored by subsequent calls to --save-baseline, but may be preserved in future Phan releases)
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
/**
3+
* Unit Tests for Util class.
4+
*
5+
* @package automattic/jetpack-forms
6+
*/
7+
8+
namespace Automattic\Jetpack\Forms\ContactForm;
9+
10+
use PHPUnit\Framework\Attributes\CoversClass;
11+
use WorDBless\BaseTestCase;
12+
13+
/**
14+
* Test class for Util
15+
*
16+
* @covers Automattic\Jetpack\Forms\ContactForm\Util
17+
*/
18+
#[CoversClass( Util::class )]
19+
class Util_Test extends BaseTestCase {
20+
21+
/**
22+
* Test that Admin::init() is not called when Util::init() is called.
23+
*
24+
* This test verifies that the deprecated Admin class is not being initialized
25+
* when the Util class is initialized, confirming the deprecation of the
26+
* classic admin functionality.
27+
*/
28+
public function test_admin_init_not_called_on_util_init() {
29+
// Initialize Util
30+
Util::init();
31+
32+
$actions_after = $GLOBALS['wp_filter'];
33+
34+
// Verify that no admin-specific hooks were added
35+
// We check for hooks that would be added by Admin::init()
36+
$admin_hooks = array(
37+
'media_buttons',
38+
'wp_ajax_grunion_form_builder',
39+
'admin_print_styles',
40+
'admin_print_scripts',
41+
'admin_head',
42+
'admin_init',
43+
'admin_enqueue_scripts',
44+
'admin_footer-edit.php',
45+
);
46+
47+
$unexpected_hooks_found = array();
48+
foreach ( $admin_hooks as $hook ) {
49+
if ( isset( $actions_after[ $hook ] ) ) {
50+
// Check if any of the callbacks are from the Admin class
51+
foreach ( $actions_after[ $hook ]->callbacks as $callbacks ) {
52+
foreach ( $callbacks as $callback ) {
53+
if ( is_array( $callback['function'] ) &&
54+
is_object( $callback['function'][0] ) &&
55+
$callback['function'][0] instanceof Admin ) {
56+
$unexpected_hooks_found[] = $hook;
57+
}
58+
}
59+
}
60+
}
61+
}
62+
63+
$this->assertEmpty(
64+
$unexpected_hooks_found,
65+
'No Admin class hooks should be registered after Util::init(). Found: ' . implode( ', ', $unexpected_hooks_found )
66+
);
67+
}
68+
69+
/**
70+
* Test that Admin::init() method is properly deprecated.
71+
*
72+
* This test verifies that calling Admin::init() directly triggers
73+
* a deprecation notice as expected.
74+
*/
75+
public function test_admin_init_is_deprecated() {
76+
// Test that the Admin::init method has the @deprecated annotation
77+
$reflection = new \ReflectionMethod( Admin::class, 'init' );
78+
$doc_comment = $reflection->getDocComment();
79+
80+
$this->assertStringContainsString( '@deprecated', $doc_comment, 'Admin::init() method should have @deprecated annotation' );
81+
82+
// Call the deprecated method and verify it still works (for backward compatibility)
83+
$admin_instance = Admin::init();
84+
$this->assertInstanceOf( Admin::class, $admin_instance, 'Admin::init() should still return an Admin instance for backward compatibility' );
85+
}
86+
87+
/**
88+
* Test that Util::init() sets up the expected hooks and filters.
89+
*
90+
* This test verifies that the Util::init() method properly registers
91+
* the expected WordPress hooks and filters without initializing the
92+
* deprecated Admin class.
93+
*/
94+
public function test_util_init_registers_expected_hooks() {
95+
// Remove any existing hooks first to get a clean state
96+
remove_all_filters( 'template_include' );
97+
remove_all_actions( 'render_block_core_template_part_post' );
98+
remove_all_actions( 'init' );
99+
remove_all_actions( 'grunion_scheduled_delete' );
100+
remove_all_actions( 'grunion_pre_message_sent' );
101+
102+
// Initialize Util
103+
Util::init();
104+
105+
// Verify that the expected hooks are registered (has_filter/has_action return priority or false)
106+
$this->assertNotFalse(
107+
has_filter( 'template_include', '\Automattic\Jetpack\Forms\ContactForm\Util::grunion_contact_form_set_block_template_attribute' ),
108+
'template_include filter should be registered'
109+
);
110+
111+
$this->assertNotFalse(
112+
has_action( 'render_block_core_template_part_post', '\Automattic\Jetpack\Forms\ContactForm\Util::grunion_contact_form_set_block_template_part_id_global' ),
113+
'render_block_core_template_part_post action should be registered'
114+
);
115+
116+
$this->assertNotFalse(
117+
has_action( 'init', '\Automattic\Jetpack\Forms\ContactForm\Contact_Form_Plugin::init' ),
118+
'Contact_Form_Plugin::init should be registered on init action'
119+
);
120+
121+
$this->assertNotFalse(
122+
has_action( 'grunion_scheduled_delete', '\Automattic\Jetpack\Forms\ContactForm\Util::grunion_delete_old_spam' ),
123+
'grunion_scheduled_delete action should be registered'
124+
);
125+
126+
$this->assertNotFalse(
127+
has_action( 'grunion_pre_message_sent', '\Automattic\Jetpack\Forms\ContactForm\Util::jetpack_tracks_record_grunion_pre_message_sent' ),
128+
'grunion_pre_message_sent action should be registered'
129+
);
130+
}
131+
}

0 commit comments

Comments
 (0)