| 1 | <?php |
| 2 | |
| 3 | require_once( ABSPATH . 'wp-admin/includes/nav-menu.php' ); |
| 4 | |
| 5 | /** |
| 6 | * @group admin |
| 7 | */ |
| 8 | class Tests_Admin_NavMenus extends WP_UnitTestCase |
| 9 | { |
| 10 | |
| 11 | private $menu_items_sorted_by_position; |
| 12 | private $user_id; |
| 13 | |
| 14 | function setUp() |
| 15 | { |
| 16 | parent::setUp(); |
| 17 | |
| 18 | $this->user_id = $this->factory->user->create( array( 'role' => 'author' ) ); |
| 19 | |
| 20 | $this->menu_items_sorted_by_position = array(); |
| 21 | $item = $this->create_menu_item_for_test( $this->user_id, "A", 1, 0 ); |
| 22 | $this->menu_items_sorted_by_position[ $item->menu_order ] = $item; |
| 23 | $item = $this->create_menu_item_for_test( $this->user_id, "B", 2, $item->db_id ); |
| 24 | $this->menu_items_sorted_by_position[ $item->menu_order ] = $item; |
| 25 | $parent_id = $item->db_id; |
| 26 | $item = $this->create_menu_item_for_test( $this->user_id, "C", 3, $parent_id ); |
| 27 | $this->menu_items_sorted_by_position[ $item->menu_order ] = $item; |
| 28 | $item = $this->create_menu_item_for_test( $this->user_id, "D", 4, $parent_id ); |
| 29 | $this->menu_items_sorted_by_position[ $item->menu_order ] = $item; |
| 30 | $item = $this->create_menu_item_for_test( $this->user_id, "E", 5, 0 ); |
| 31 | $this->menu_items_sorted_by_position[ $item->menu_order ] = $item; |
| 32 | $item = $this->create_menu_item_for_test( $this->user_id, "F", 6, $item->db_id ); |
| 33 | $this->menu_items_sorted_by_position[ $item->menu_order ] = $item; |
| 34 | $item = $this->create_menu_item_for_test( $this->user_id, "G", 7, $item->db_id ); |
| 35 | $this->menu_items_sorted_by_position[ $item->menu_order ] = $item; |
| 36 | $item = $this->create_menu_item_for_test( $this->user_id, "H", 8, 0 ); |
| 37 | $this->menu_items_sorted_by_position[ $item->menu_order ] = $item; |
| 38 | } |
| 39 | |
| 40 | function test_wp_update_menu_item_positions_nothing_changed() |
| 41 | { |
| 42 | $unchangedMenuItem = array(); |
| 43 | $changed_menu_items = array(); |
| 44 | foreach ( $this->menu_items_sorted_by_position as $item ) { |
| 45 | $unchangedMenuItem[ $item->db_id ] = $item; |
| 46 | } |
| 47 | wp_update_menu_item_positions( $this->menu_items_sorted_by_position, $unchangedMenuItem, $changed_menu_items ); |
| 48 | $this->assert_order( array( 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H' ) ); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * test to move menu item E from position 5 to 1. Expected order: |
| 53 | * E, F, G, A, B, C, D, H |
| 54 | */ |
| 55 | function test_wp_update_menu_item_positions_simple_change() |
| 56 | { |
| 57 | $unchanged_menu_items = array(); |
| 58 | $changed_menu_items = array(); |
| 59 | foreach ( $this->menu_items_sorted_by_position as $item ) { |
| 60 | if ( $item->post_title == "E" ) { |
| 61 | $changed_menu_items[ 1 ] = $this->update_menu_item( $item, 1 ); |
| 62 | } else { |
| 63 | $unchanged_menu_items[ $item->db_id ] = $item; |
| 64 | } |
| 65 | } |
| 66 | wp_update_menu_item_positions( $this->menu_items_sorted_by_position, $unchanged_menu_items, $changed_menu_items ); |
| 67 | $this->assert_order( array( 'E', 'F', 'G', 'A', 'B', 'C', 'D', 'H' ) ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * |
| 72 | */ |
| 73 | function test_wp_update_menu_item_positions_complex_change() |
| 74 | { |
| 75 | $unchanged_menu_items = array(); |
| 76 | $changed_menu_items = array(); |
| 77 | foreach ( $this->menu_items_sorted_by_position as $item ) { |
| 78 | if ( $item->post_title == "C" ) { |
| 79 | $changed_menu_items[ 1 ] = $this->update_menu_item( $item, 1 ); |
| 80 | } elseif ( $item->post_title == "F" ) { |
| 81 | $changed_menu_items[ 2 ] = $this->update_menu_item( $item, 2 ); |
| 82 | } elseif ( $item->post_title == "A" ) { |
| 83 | $changed_menu_items[ 5 ] = $this->update_menu_item( $item, 5 ); |
| 84 | } elseif ( $item->post_title == "D" ) { |
| 85 | $changed_menu_items[ 8 ] = $this->update_menu_item( $item, 8 ); |
| 86 | } else { |
| 87 | $unchanged_menu_items[ $item->db_id ] = $item; |
| 88 | } |
| 89 | } |
| 90 | wp_update_menu_item_positions( $this->menu_items_sorted_by_position, $unchanged_menu_items, $changed_menu_items ); |
| 91 | $this->assert_order( array( 'C', 'F', 'G', 'E', 'A', 'B', 'H', 'D' ) ); |
| 92 | } |
| 93 | |
| 94 | function test_wp_update_menu_item_positions_insert_new() |
| 95 | { |
| 96 | $unchanged_menu_items = array(); |
| 97 | $changed_menu_items = array(); |
| 98 | foreach ( $this->menu_items_sorted_by_position as $item ) { |
| 99 | if ( $item->post_title == 'A' ) { |
| 100 | $new_item = $this->create_menu_item_for_test( $this->user_id, 'A1', 2, $item->db_id ); |
| 101 | $changed_menu_items[ 2 ] = $this->update_menu_item( $new_item, 2 ); |
| 102 | } elseif ( $item->post_title == 'B' ) { |
| 103 | $new_item = $this->create_menu_item_for_test( $this->user_id, 'B1', 4, $item->db_id ); |
| 104 | $changed_menu_items[ 4 ] = $this->update_menu_item( $new_item, 4 ); |
| 105 | } elseif ( $item->post_title == 'D' ) { |
| 106 | $new_item = $this->create_menu_item_for_test( $this->user_id, 'D1', 7, $item->db_id ); |
| 107 | $changed_menu_items[ 7 ] = $this->update_menu_item( $new_item, 7 ); |
| 108 | $new_item = $this->create_menu_item_for_test( $this->user_id, 'D11', 8, $new_item->db_id ); |
| 109 | $changed_menu_items[ 8 ] = $this->update_menu_item( $new_item, 8 ); |
| 110 | } elseif ( $item->post_title == 'G' ) { |
| 111 | $new_item = $this->create_menu_item_for_test( $this->user_id, 'G1', 12, $item->db_id ); |
| 112 | $changed_menu_items[ 12 ] = $this->update_menu_item( $new_item, 12 ); |
| 113 | $new_item = $this->create_menu_item_for_test( $this->user_id, 'G2', 13, 0 ); |
| 114 | $changed_menu_items[ 13 ] = $this->update_menu_item( $new_item, 13 ); |
| 115 | } |
| 116 | |
| 117 | $unchanged_menu_items[ $item->db_id ] = $item; |
| 118 | } |
| 119 | wp_update_menu_item_positions( $this->menu_items_sorted_by_position, $unchanged_menu_items, $changed_menu_items ); |
| 120 | $this->assert_order( array( 'A', 'A1', 'B', 'B1', 'C', 'D', 'D1', 'D11', 'E', 'F', 'G', 'G1', 'G2', 'H' ) ); |
| 121 | } |
| 122 | |
| 123 | public function update_menu_item( $item, $new_position ) |
| 124 | { |
| 125 | $item->menu_order = $new_position; |
| 126 | wp_update_post( $item ); |
| 127 | $changed_menu_item = array( |
| 128 | 'db_id' => $item->db_id, |
| 129 | 'menu_order' => $item->menu_order, |
| 130 | 'menu_item_parent' => $item->menu_item_parent |
| 131 | ); |
| 132 | return (object) $changed_menu_item; |
| 133 | } |
| 134 | |
| 135 | |
| 136 | private function create_menu_item_for_test( $user_id, $title, $menu_order, $menu_item_parent = 0 ) |
| 137 | { |
| 138 | $post = array( |
| 139 | 'post_author' => $user_id, |
| 140 | 'post_status' => 'publish', |
| 141 | 'post_content' => rand_str(), |
| 142 | 'post_title' => $title, |
| 143 | 'post_type' => 'nav_menu_item', |
| 144 | 'menu_order' => $menu_order, |
| 145 | ); |
| 146 | |
| 147 | $nav_id = wp_insert_post( $post ); |
| 148 | $post = get_post( $nav_id ); |
| 149 | $post->db_id = $nav_id; |
| 150 | $post->menu_item_parent = $menu_item_parent; |
| 151 | return $post; |
| 152 | } |
| 153 | |
| 154 | private function assert_order( $new_order ) |
| 155 | { |
| 156 | foreach ( $this->menu_items_sorted_by_position as $item ) { |
| 157 | $post = get_post( $item->db_id ); |
| 158 | $expected_position = array_search( $item->post_title, $new_order ) + 1; |
| 159 | $this->assertEquals( $expected_position, $post->menu_order, "Menu order is wrong" ); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | } |