/** * * * @since 1.1.0 * * @group one * @group group * @group destination * * @covers \ingot\testing\tests\click\destination\hooks::track_by_id() */ public function testPageTracking() { $post = get_post(wp_insert_post(['post_title' => 'Hi Chris'])); $args = ['name' => rand(), 'type' => 'click', 'sub_type' => 'destination', 'meta' => ['destination' => 'page', 'link' => 'https://bats.com', 'page' => 11, 'is_tagline' => true]]; $group_id = \ingot\testing\crud\group::create($args, true); $group = \ingot\testing\crud\group::read($group_id); $content_1 = 'hats'; $args = ['type' => 'click', 'group_ID' => $group_id, 'content' => $content_1]; $variant_1 = \ingot\testing\crud\variant::create($args, true); $content_2 = 'bats'; $args = ['type' => 'click', 'group_ID' => $group_id, 'content' => $content_2]; $variant_2 = \ingot\testing\crud\variant::create($args, true); $group['variants'] = [$variant_1, $variant_2]; $update = \ingot\testing\crud\group::update($group, $group_id); $this->assertEquals($group_id, $update); $group = \ingot\testing\crud\group::read($group_id); $this->assertTrue(is_array($group)); $this->assertFalse(empty($group['variants'])); new \ingot\testing\bandit\content($group_id); $group = \ingot\testing\crud\group::read($group_id); $this->assertFalse(empty($group['levers'])); $groups = \ingot\testing\tests\click\destination\init::set_tracking(); new \ingot\testing\tests\click\destination\hooks($groups); $variant_id = \ingot\testing\tests\click\destination\init::get_test($group_id); $tracking = new \ingot\testing\tests\click\destination\hooks([$group_id]); $tracking->track_by_id(11); $totals = \ingot\testing\utility\group::get_total($group, true); $this->assertEquals(1, $totals['total']); $this->assertEquals(1, $totals['conversions']); }
/** * Create groups with variants, optionally added to them. * * @param bool $add_variants Optional. If true, the default, variants are added to groups via groups object. If false, they are created, IDs are returned but they will nto be associated with a group properly. * @param int $total_groups Optional. Default is 5. * @param int $variants_per_group Optional. Default is 3. * @param array $args Optional * * @return array */ public static function make_groups($add_variants = true, $total_groups = 5, $variants_per_group = 3, $args = array()) { $defaults = ['group_args' => ['name' => rand(), 'type' => 'click', 'sub_type' => 'button', 'meta' => ['link' => 'https://bats.com']], 'variant_args' => ['type' => 'click', 'group_ID' => 1, 'content' => rand()]]; $keys = array_keys($defaults); foreach ($keys as $i => $key) { if (!empty($args[$key])) { $args[$key] = wp_parse_args($args[$key], $defaults[$key]); } else { $args[$key] = $defaults[$key]; } } $groups = ['ids' => [], 'variants' => []]; $group_args = $args['group_args']; $variant_args = $args['variant_args']; for ($g = 0; $g <= $total_groups; $g++) { $variants = []; $group_args['name'] = (string) $g . rand(); $group_id = \ingot\testing\crud\group::create($group_args, true); $groups['ids'][] = $group_id; $variant_args['group_ID'] = $group_id; for ($v = 0; $v <= $variants_per_group; $v++) { $variant_id = \ingot\testing\crud\variant::create($variant_args, true); $variants[] = $variant_id; } $groups['variants'][$group_id] = $variants; if ($add_variants) { if (is_user_logged_in()) { $obj = new \ingot\testing\object\group($group_id); $obj->update_group(['variants' => $variants]); } else { $_group = \ingot\testing\crud\group::read($group_id); $_group['variants'] = $variants; $saved = \ingot\testing\crud\group::update($_group, $group_id, true); } } } return $groups; }
/** * Test that EDD sale of non-variable product is properly priced/recorded * * @since 1.1.0 * * @group pricez * @group edd_price * * @covers \ingot\testing\tests\price\plugins\edd */ public function testPriceTrackingNonVariableProduct() { $price_is = 10; $product = ingot_test_data_price::edd_create_simple_download($price_is); $group_id = \ingot\testing\crud\group::create(['type' => 'price', 'sub_type' => 'edd', 'meta' => ['product_ID' => $product->ID], 'wp_ID' => $product->ID], true); $variant_one = \ingot\testing\crud\variant::create(['group_ID' => $group_id, 'type' => 'price', 'meta' => ['price' => [0.5]], 'content' => $product->ID], true); $variant_two = \ingot\testing\crud\variant::create(['group_ID' => $group_id, 'type' => 'price', 'meta' => ['price' => [0.5]], 'content' => $product->ID], true); $group = \ingot\testing\crud\group::read($group_id); $group['variants'] = [$variant_one, $variant_two]; \ingot\testing\crud\group::update($group, $group_id, true); $cookie_class = new \ingot\testing\cookies\price([]); $price_cookie = $cookie_class->get_cookie(); $this->assertArrayHasKey('edd', $price_cookie); $this->assertFalse(empty($price_cookie['edd'])); $this->assertInternalType('array', $price_cookie['edd']); $product_id = \ingot\testing\utility\price::get_product_ID($group); $this->assertEquals($product_id, $product->ID); $this->assertArrayHasKey($product_id, $price_cookie['edd']); new \ingot\testing\tests\price\plugins\edd($price_cookie['edd']); $test = \ingot\testing\utility\price::get_price_test_from_cookie('edd', $product->ID, $price_cookie); $this->assertInternalType('object', $test); $price_should_be = $test->get_price(); //NOTE: USING edd_get_download_price here is to ensure we don't have recursion $this->assertEquals(edd_get_download_price($product->ID), $price_should_be); $group_obj = new \ingot\testing\object\group($group_id); $lever = $group_obj->get_lever($test->ID); $this->assertInternalType('object', $lever); $before_wins = $lever->getNumerator(); $payment_id = ingot_test_data_price::edd_create_simple_payment($product); edd_complete_purchase($payment_id, 'publish', 'pending'); $group_obj = new \ingot\testing\object\group($group_id); $lever = $group_obj->get_lever($test->ID); $this->assertInternalType('object', $lever); $after_wins = $lever->getNumerator(); $this->assertEquals($before_wins + 1, $after_wins); }
/** * Update group config * * Note, as opposed to using \ingot\testing\crud\group::update() directly, you do not need to pass all fields, just the ones to be changed. Also, the config in this object will be updated. * * @since 0.4.0 * * @param array $data * * @return bool|int|\WP_Error True if updated, or bool if couldn't save or WP_Error if not allowed. */ public function update_group($data) { $allowed = $this->all_fields(); foreach (array_keys($data) as $key) { if (!in_array($key, $allowed)) { unset($data[$key]); } } $group = wp_parse_args($data, $this->group); $saved = \ingot\testing\crud\group::update($group, $this->ID, $this->bypass_cap); if (is_numeric($saved)) { $this->set_group($group); $this->set_levers(); return true; } else { return $saved; } }
/** * Create the variants and save in group * * @param int $group_id The group ID * * @return int|WP_Error Should return group ID. May return WP_Error if there was an error creating variants or saving them in group. */ protected function add_variants($group_id) { $variant_args = ['type' => 'click', 'group_ID' => $group_id]; /** * This array will be the HTML we output for each variant. * * As an example I'm using URLs of cute cate photos * * @TODO -- You should use something meaningful here. */ $variant_contents = ['https://placekitten.com/900/300', 'https://placekitten.com/900/200', 'https://placekitten.com/800/400']; $variant_ids = []; //create the variants foreach ($variant_contents as $content) { $variant_args['content'] = $content; $variant_id = \ingot\testing\crud\variant::create($variant_args); if (is_numeric($variant_id)) { $variant_ids[] = $variant_id; } else { return $variant_id; } } //save variant IDs in group $group = \ingot\testing\crud\group::read($group_id); $group['variants'] = $variant_ids; $saved = \ingot\testing\crud\group::update($group, $group_id); //this should be the group ID, could be an error. return $saved; }
/** * Test that we can query by plugin and exclude those without variants * * @since 1.1.0 * * @group xs * @group price_query * @group price * * @covers \ingot\testing\crud\price_query::find_by_plugin() */ public function testQueryByPluginNoVariant() { $args = ['type' => 'price', 'sub_type' => 'edd', 'meta' => ['product_ID' => 5], 'wp_ID' => 5]; $variant_args = ['type' => 'price', 'meta' => ['price' => []], 'content' => 5]; for ($i = 0; $i <= 5; $i++) { $group_id = \ingot\testing\crud\group::create($args); $args['wp_ID'] = $args['wp_ID'] + 1; if (3 == $i) { $expected_id = $group_id; $variant_args['group_ID'] = $group_id; $variant_args['meta']['price'] = rand_float(); $variant_id = \ingot\testing\crud\variant::create($variant_args, true); $this->assertTrue(is_numeric($variant_id)); $group = \ingot\testing\crud\group::read($group_id); $group['variants'] = [$variant_id]; \ingot\testing\crud\group::update($group, $group_id, true); } } $groups = \ingot\testing\crud\price_query::find_by_plugin('edd', true); $this->assertInternalType('array', $groups); $this->assertFalse(empty($groups)); $ids = wp_list_pluck($groups, 'ID'); $this->assertTrue(in_array($expected_id, $ids)); $this->assertSame(1, count($groups)); }
/** * Test that when we can add variants to groups properly * * @since 0.4.0 * * @group crud * @group group * @group group_crud * * @covers \ingot\testing\crud\group::update() */ public function testAddVariantsToGroup() { $groups = ingot_tests_make_groups(false, 1, 3); $id = $groups['ids'][0]; $this->assertTrue(is_numeric($id)); $group = \ingot\testing\crud\group::read($id); $group['variants'] = $groups['variants'][$id]; $updated = \ingot\testing\crud\group::update($group, $id); $this->assertTrue(is_numeric($updated)); $group = \ingot\testing\crud\group::read($id); $this->assertSame($group['variants'], $groups['variants'][$id]); }
/** * Create a group * * @since 0.4.0 * * @param \WP_REST_Request $request Full data about the request. * @return \WP_Error|\WP_REST_Response */ public function create_item($request) { $variants = helpers::sanitize($request->get_param('variants')); $group_args = $this->prepare_item_for_database($request, true); $group_args['variants'] = []; $id = group::create($group_args); $item = new \WP_Error('ingot-unknown-error'); if (is_wp_error($id)) { $item = $id; } if (is_numeric($id)) { $item = group::read($id); $item['variants'] = $variants; if (is_array($item)) { $variants_ids = $this->save_variants($item); if (is_wp_error($variants_ids)) { return ingot_rest_response($variants_ids, 500); } $item['variants'] = $variants_ids; group::update($item, $item['ID']); $item = group::read($item['ID']); if (is_array($item)) { $item = $this->prepare_group($item, $request->get_param('context')); return ingot_rest_response($item, 201, 1); } } } return ingot_rest_response($item, 500); }