<?php


class Wiaas_Templates_Test extends Wiaas_Unit_Test_Case {

    var $template, $product, $package, $template_category, $template_meta;

    public function setUp() {
        parent::setUp();

        # set admin as current user
        wp_set_current_user(1);

        $this->template_category = wp_create_term('Test', 'template_category');
        $this->template_meta = array(
            'template_category_id' => $this->template_category['term_id'],
            'template_category_title' => 'Test',
            'quantity' => 1
        );

        $this->template = $this->create_new_wiaas_template();
        $this->product = $this->create_new_product();
        $this->package = $this->create_new_package();
        $this->add_products_to_package($this->package, $this->product);


    }

    public function test_template_category_taxonomy_created() {
        $taxonomy = get_taxonomy('template_category');

        $this->assertInstanceOf(WP_Taxonomy::class, $taxonomy);

        $template_category_names = array_map(function ($term) {
            return $term->name;
        }, get_terms(array('taxonomy' => 'template_category', 'hide_empty' => false)));

        $this->assertNotEmpty($template_category_names);
        $this->assertContains('Test', $template_category_names);

    }

    function test_adding_template_category_to_template() {

        WC_Product_Template::save_template_product_meta($this->template->get_id(), 'hardware', $this->template_meta);

        $test_template_category = WC_Product_Template::get_template_categories_from_meta(
            $this->template->get_id(), '_template_items_hardware');

        $this->assertEquals($test_template_category['template_category_id'], $this->template_meta['template_category_id']);
        $this->assertEquals($test_template_category['template_category_title'], $this->template_meta['template_category_title']);
        $this->assertEquals($test_template_category['quantity'], $this->template_meta['quantity']);
    }

    function save_bind_package_to_template() {
        WC_Product_Template::bind_selected_template_to_product($this->template->get_id(), $this->package->get_id());
        $value = get_post_meta($this->package->get_id(), '_select_template', true);

        $this->assertNotEmpty($value);
    }
}