|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests for the Menus Connector class callbacks. |
| 4 | + */ |
| 5 | +namespace WP_Stream; |
| 6 | + |
| 7 | +class Test_WP_Stream_Connector_Menus extends WP_StreamTestCase { |
| 8 | + /** |
| 9 | + * Runs before each test |
| 10 | + */ |
| 11 | + public function setUp() { |
| 12 | + parent::setUp(); |
| 13 | + |
| 14 | + // Make partial of Connector_ACF class, with mocked "log" function. |
| 15 | + $this->mock = $this->getMockBuilder( Connector_Menus::class ) |
| 16 | + ->setMethods( array( 'log' ) ) |
| 17 | + ->getMock(); |
| 18 | + |
| 19 | + // Register connector. |
| 20 | + $this->mock->register(); |
| 21 | + } |
| 22 | + |
| 23 | + public function test_callback_wp_create_nav_menu() { |
| 24 | + // Expected log calls. |
| 25 | + $this->mock->expects( $this->exactly( 1 ) ) |
| 26 | + ->method( 'log' ) |
| 27 | + ->with( |
| 28 | + $this->equalTo( __( 'Created new menu "%s"', 'stream' ) ), |
| 29 | + $this->callback( |
| 30 | + function( $subject ) { |
| 31 | + $expected = array( 'name' => 'test-menu' ); |
| 32 | + return $expected === array_intersect_key( $expected, $subject ); |
| 33 | + } |
| 34 | + ), |
| 35 | + $this->greaterThan( 0 ), |
| 36 | + $this->equalTo( 'test-menu' ), |
| 37 | + $this->equalTo( 'created' ) |
| 38 | + ); |
| 39 | + |
| 40 | + // Create nav menu to trigger callback. |
| 41 | + wp_create_nav_menu( 'test-menu' ); |
| 42 | + |
| 43 | + // Check callback test action. |
| 44 | + $this->assertGreaterThan( 0, did_action( 'wp_stream_test_callback_wp_create_nav_menu' ) ); |
| 45 | + } |
| 46 | + |
| 47 | + public function test_callback_wp_update_nav_menu() { |
| 48 | + // Create nav menu for later use. |
| 49 | + $menu_id = wp_create_nav_menu( 'test-menu' ); |
| 50 | + |
| 51 | + // Expected log calls. |
| 52 | + $this->mock->expects( $this->exactly( 1 ) ) |
| 53 | + ->method( 'log' ) |
| 54 | + ->with( |
| 55 | + $this->equalTo( _x( 'Updated menu "%s"', 'Menu name', 'stream' ) ), |
| 56 | + $this->equalTo( |
| 57 | + array( |
| 58 | + 'name' => 'test-menu', |
| 59 | + 'menu_id' => $menu_id, |
| 60 | + 'menu_data' => array( |
| 61 | + 'description' => 'yo', |
| 62 | + 'menu-name' => 'test-menu', |
| 63 | + ), |
| 64 | + ) |
| 65 | + ), |
| 66 | + $this->equalTo( $menu_id ), |
| 67 | + $this->equalTo( 'test-menu' ), |
| 68 | + $this->equalTo( 'updated' ) |
| 69 | + ); |
| 70 | + |
| 71 | + // Update nav menu to trigger callback. |
| 72 | + wp_update_nav_menu_object( |
| 73 | + $menu_id, |
| 74 | + array( |
| 75 | + 'description' => 'yo', |
| 76 | + 'menu-name' => 'test-menu', |
| 77 | + ) |
| 78 | + ); |
| 79 | + |
| 80 | + // Check callback test action. |
| 81 | + $this->assertGreaterThan( 0, did_action( 'wp_stream_test_callback_wp_update_nav_menu' ) ); |
| 82 | + } |
| 83 | + |
| 84 | + public function test_callback_delete_nav_menu() { |
| 85 | + // Unregister Menus connector to avoid callback conflicts. |
| 86 | + $this->plugin->connectors->unload_connector( 'menus' ); |
| 87 | + |
| 88 | + // Create nav menu for later use. |
| 89 | + $menu_id = wp_create_nav_menu( 'test-menu' ); |
| 90 | + |
| 91 | + // Expected log calls. |
| 92 | + $this->mock->expects( $this->exactly( 1 ) ) |
| 93 | + ->method( 'log' ) |
| 94 | + ->with( |
| 95 | + $this->equalTo( _x( 'Deleted "%s"', 'Menu name', 'stream' ) ), |
| 96 | + $this->equalTo( |
| 97 | + array( |
| 98 | + 'name' => 'test-menu', |
| 99 | + 'menu_id' => $menu_id, |
| 100 | + ) |
| 101 | + ), |
| 102 | + $this->equalTo( $menu_id ), |
| 103 | + $this->equalTo( 'test-menu' ), |
| 104 | + $this->equalTo( 'deleted' ) |
| 105 | + ); |
| 106 | + |
| 107 | + // Delete nav menu to trigger callback. |
| 108 | + wp_delete_nav_menu( $menu_id ); |
| 109 | + |
| 110 | + // Check callback test action. |
| 111 | + $this->assertGreaterThan( 0, did_action( 'wp_stream_test_callback_delete_nav_menu' ) ); |
| 112 | + } |
| 113 | + |
| 114 | + public function test_callback_update_option_theme_mods() { |
| 115 | + // Create nav menu and nav menu location for later use. |
| 116 | + $menu_id = wp_create_nav_menu( 'test-menu' ); |
| 117 | + register_nav_menu( 'main', 'Main Navigation' ); |
| 118 | + |
| 119 | + // Create theme mods options for later use. |
| 120 | + $locations = get_theme_mod('nav_menu_locations'); |
| 121 | + $locations['main'] = ''; |
| 122 | + set_theme_mod( 'nav_menu_locations', $locations ); |
| 123 | + |
| 124 | + // Expected log calls. |
| 125 | + $this->mock->expects( $this->exactly( 2 ) ) |
| 126 | + ->method( 'log' ) |
| 127 | + ->withConsecutive( |
| 128 | + array( |
| 129 | + $this->equalTo( |
| 130 | + _x( |
| 131 | + '"%1$s" has been assigned to "%2$s"', |
| 132 | + '1: Menu name, 2: Theme location', |
| 133 | + 'stream' |
| 134 | + ) |
| 135 | + ), |
| 136 | + $this->equalTo( |
| 137 | + array( |
| 138 | + 'name' => 'test-menu', |
| 139 | + 'location' => 'Main Navigation', |
| 140 | + 'location_id' => 'main', |
| 141 | + 'menu_id' => $menu_id, |
| 142 | + ) |
| 143 | + ), |
| 144 | + $this->equalTo( $menu_id ), |
| 145 | + $this->equalTo( 'test-menu' ), |
| 146 | + $this->equalTo( 'assigned' ) |
| 147 | + ), |
| 148 | + array( |
| 149 | + $this->equalTo( |
| 150 | + _x( |
| 151 | + '"%1$s" has been unassigned from "%2$s"', |
| 152 | + '1: Menu name, 2: Theme location', |
| 153 | + 'stream' |
| 154 | + ) |
| 155 | + ), |
| 156 | + $this->equalTo( |
| 157 | + array( |
| 158 | + 'name' => 'test-menu', |
| 159 | + 'location' => 'Main Navigation', |
| 160 | + 'location_id' => 'main', |
| 161 | + 'menu_id' => $menu_id, |
| 162 | + ) |
| 163 | + ), |
| 164 | + $this->equalTo( $menu_id ), |
| 165 | + $this->equalTo( 'test-menu' ), |
| 166 | + $this->equalTo( 'unassigned' ) |
| 167 | + ) |
| 168 | + ); |
| 169 | + |
| 170 | + // Assign/Unassigned menu to a theme nav menu location to trigger callback. |
| 171 | + $locations['main'] = $menu_id; |
| 172 | + set_theme_mod( 'nav_menu_locations', $locations ); |
| 173 | + |
| 174 | + $locations['main'] = ''; |
| 175 | + set_theme_mod( 'nav_menu_locations', $locations ); |
| 176 | + |
| 177 | + // No test action for this callback. |
| 178 | + } |
| 179 | +} |
0 commit comments