Example #1
0
 /**
  * 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;
 }
Example #2
0
 /**
  * Test that when we can update other stuff through group object
  *
  * @since 0.4.0
  *
  * @group group
  * @group group_object
  *
  * @covers \ingot\testing\object\group::get_group_config()
  * @covers \ingot\testing\object\group::update_group()
  */
 public function testUpdateThroughObject()
 {
     $groups = ingot_tests_make_groups(true, 1, 3);
     $id = $groups['ids'][0];
     $this->assertTrue(is_numeric($id));
     $variants = $groups['variants'][$id];
     $obj = new \ingot\testing\object\group($id);
     $this->assertEquals($variants, $obj->get_group_config()['variants']);
     $new_data = ['name' => 'BATMAN'];
     $obj->update_group($new_data);
     $group = \ingot\testing\crud\group::read($id);
     $this->assertEquals('BATMAN', $group['name']);
     $new_data = ['name' => 'Hi Roy', 'sub_type' => 'button_color'];
     $obj->update_group($new_data);
     $group = \ingot\testing\crud\group::read($id);
     $this->assertEquals('Hi Roy', $group['name']);
     $this->assertEquals('button_color', $group['sub_type']);
 }
Example #3
0
 /**
  * Update a group
  *
  * @since 0.4.0
  *
  * @param \WP_REST_Request $request Full data about the request.
  * @return \WP_Error|\WP_REST_Response
  */
 public function update_item($request)
 {
     $url = $request->get_url_params();
     $id = helpers::v('id', $url, 0);
     $existing = group::read($id);
     if (!is_array($existing)) {
         if (is_wp_error($existing)) {
             return $existing;
         }
         return ingot_rest_response(['message' => esc_html__('No group found', 'ingot')]);
     }
     $obj = new \ingot\testing\object\group($existing);
     $group_args = $this->prepare_item_for_database($request);
     $obj->update_group($group_args);
     return ingot_rest_response($this->prepare_group($obj->get_group_config(), $request->get_param('context')), 201);
 }