|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests for the WC_Order_Data_Store_Custom_Table class. |
| 4 | + * |
| 5 | + * @package Woocommerce_Order_Tables |
| 6 | + * @author Liquid Web |
| 7 | + */ |
| 8 | + |
| 9 | +class DataStoreTest extends TestCase { |
| 10 | + |
| 11 | + /** |
| 12 | + * Fire the necessary actions to bootstrap WordPress. |
| 13 | + * |
| 14 | + * @before |
| 15 | + */ |
| 16 | + public function init() { |
| 17 | + do_action( 'init' ); |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * Remove any closures that have been assigned to filters. |
| 22 | + * |
| 23 | + * @after |
| 24 | + */ |
| 25 | + public function remove_filter_callbacks() { |
| 26 | + remove_all_filters( 'woocommerce_shop_order_search_fields' ); |
| 27 | + } |
| 28 | + |
| 29 | + public function test_create() { |
| 30 | + $instance = new WC_Order_Data_Store_Custom_Table(); |
| 31 | + $order = $this->factory()->order->create_and_get(); |
| 32 | + $order_key = 'my_custom_order_key'; |
| 33 | + |
| 34 | + add_filter( 'woocommerce_generate_order_key', function () use ( $order_key ) { |
| 35 | + return $order_key; |
| 36 | + } ); |
| 37 | + |
| 38 | + $instance->create( $order ); |
| 39 | + |
| 40 | + $this->assertEquals( 'wc_' . $order_key, $order->get_order_key() ); |
| 41 | + } |
| 42 | + |
| 43 | + public function test_get_order_count() { |
| 44 | + $instance = new WC_Order_Data_Store_Custom_Table(); |
| 45 | + $orders = $this->factory()->order->create_many( 5, array( |
| 46 | + 'post_status' => 'wc-pending', |
| 47 | + ) ); |
| 48 | + |
| 49 | + $this->assertEquals( |
| 50 | + count( $orders ), |
| 51 | + $instance->get_order_count( 'wc-pending' ) |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + public function test_get_order_count_filters_by_status() { |
| 56 | + $instance = new WC_Order_Data_Store_Custom_Table(); |
| 57 | + $this->factory()->order->create( array( |
| 58 | + 'post_status' => 'not_a_pending_status', |
| 59 | + ) ); |
| 60 | + |
| 61 | + $this->assertEquals( |
| 62 | + 0, |
| 63 | + $instance->get_order_count( 'wc-pending' ), |
| 64 | + 'The get_order_count() method should only count records matching $status.' |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + public function test_get_unpaid_orders() { |
| 69 | + $instance = new WC_Order_Data_Store_Custom_Table(); |
| 70 | + $order = $this->factory()->order->create( array( |
| 71 | + 'post_status' => 'wc-pending', |
| 72 | + ) ); |
| 73 | + $pending = $instance->get_unpaid_orders( time() + DAY_IN_SECONDS ); |
| 74 | + |
| 75 | + $this->assertCount( 1, $pending, 'There should be only one unpaid order.' ); |
| 76 | + $this->assertEquals( |
| 77 | + $order, |
| 78 | + array_shift( $pending ), |
| 79 | + 'The ID of the one unpaid order should be that of $order.' |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + public function test_get_unpaid_orders_uses_date_filtering() { |
| 84 | + $instance = new WC_Order_Data_Store_Custom_Table(); |
| 85 | + $order = $this->factory()->order->create( array( |
| 86 | + 'post_status' => 'wc-pending', |
| 87 | + ) ); |
| 88 | + $pending = $instance->get_unpaid_orders( time() - HOUR_IN_SECONDS ); |
| 89 | + |
| 90 | + $this->assertEmpty( $pending, 'No unpaid orders should match the time window.' ); |
| 91 | + } |
| 92 | + |
| 93 | + public function test_search_orders_can_search_by_order_id() { |
| 94 | + $instance = new WC_Order_Data_Store_Custom_Table(); |
| 95 | + |
| 96 | + $this->assertEquals( |
| 97 | + array( 123 ), |
| 98 | + $instance->search_orders( 123 ), |
| 99 | + 'When given a numeric value, search_orders() should include that order ID.' |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + public function test_search_orders_can_check_post_meta() { |
| 104 | + $instance = new WC_Order_Data_Store_Custom_Table(); |
| 105 | + $order = $this->factory()->order->create(); |
| 106 | + $term = uniqid( 'search term ' ); |
| 107 | + |
| 108 | + add_post_meta( $order, 'some_custom_meta_key', $term ); |
| 109 | + |
| 110 | + add_filter( 'woocommerce_shop_order_search_fields', function () { |
| 111 | + return array( 'some_custom_meta_key' ); |
| 112 | + } ); |
| 113 | + |
| 114 | + $this->assertEquals( |
| 115 | + array( $order ), |
| 116 | + $instance->search_orders( $term ), |
| 117 | + 'If post meta keys are specified, they should also be searched.' |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Same as test_search_orders_can_check_post_meta(), but the filter is never added. |
| 123 | + */ |
| 124 | + public function test_search_orders_only_checks_post_meta_if_specified() { |
| 125 | + $instance = new WC_Order_Data_Store_Custom_Table(); |
| 126 | + $order = $this->factory()->order->create(); |
| 127 | + $term = uniqid( 'search term ' ); |
| 128 | + |
| 129 | + add_post_meta( $order, 'some_custom_meta_key', $term ); |
| 130 | + |
| 131 | + $this->assertEmpty( |
| 132 | + $instance->search_orders( $term ), |
| 133 | + 'Only search post meta if keys are provided.' |
| 134 | + ); |
| 135 | + } |
| 136 | + |
| 137 | + public function test_search_orders_checks_table_for_product_item_matches() { |
| 138 | + $instance = new WC_Order_Data_Store_Custom_Table(); |
| 139 | + $product = $this->factory()->product->create_and_get(); |
| 140 | + $order = $this->factory()->order->create_and_get(); |
| 141 | + $order->add_product( $product ); |
| 142 | + $order->save(); |
| 143 | + |
| 144 | + $this->assertEquals( |
| 145 | + array( $order->get_id() ), |
| 146 | + $instance->search_orders( $product->get_name() ), |
| 147 | + 'Order searches should extend to the names of product items.' |
| 148 | + ); |
| 149 | + } |
| 150 | + |
| 151 | + public function test_search_orders_checks_table_for_product_item_matches_with_like_comparison() { |
| 152 | + $instance = new WC_Order_Data_Store_Custom_Table(); |
| 153 | + $product = $this->factory()->product->create_and_get( array( |
| 154 | + 'post_title' => 'foo bar baz', |
| 155 | + ) ); |
| 156 | + $order = $this->factory()->order->create_and_get(); |
| 157 | + $order->add_product( $product ); |
| 158 | + $order->save(); |
| 159 | + |
| 160 | + $this->assertEquals( |
| 161 | + array( $order->get_id() ), |
| 162 | + $instance->search_orders( 'bar' ), |
| 163 | + 'Product items should be searched using a LIKE comparison and wildcards.' |
| 164 | + ); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * @dataProvider order_type_provider() |
| 169 | + */ |
| 170 | + public function test_get_order_type( $order_type ) { |
| 171 | + $instance = new WC_Order_Data_Store_Custom_Table(); |
| 172 | + $order = $this->factory()->order->create( array( |
| 173 | + 'post_type' => $order_type, |
| 174 | + ) ); |
| 175 | + |
| 176 | + $this->assertEquals( |
| 177 | + $order_type, |
| 178 | + $instance->get_order_type( $order ) |
| 179 | + ); |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Provide a list of all available order types. |
| 184 | + */ |
| 185 | + public function order_type_provider() { |
| 186 | + $types = array(); |
| 187 | + |
| 188 | + foreach ( wc_get_order_types() as $type ) { |
| 189 | + $types[ $type ] = array( $type ); |
| 190 | + } |
| 191 | + |
| 192 | + return $types; |
| 193 | + } |
| 194 | +} |
0 commit comments