<?php
/**
 * Unit Tests for HVAC Announcements Custom Post Type
 *
 * @package HVAC_Community_Events
 * @subpackage Tests
 */

class Test_HVAC_Announcements_CPT extends WP_UnitTestCase {
    
    /**
     * Setup before class
     */
    public static function setUpBeforeClass() {
        parent::setUpBeforeClass();
        
        // Register post type and taxonomies
        $cpt = HVAC_Announcements_CPT::get_instance();
        $cpt->register_post_type();
        $cpt->register_taxonomies();
    }
    
    /**
     * Test post type is registered
     */
    public function test_post_type_registered() {
        $post_type = HVAC_Announcements_CPT::get_post_type();
        $this->assertTrue( post_type_exists( $post_type ) );
    }
    
    /**
     * Test post type name constant
     */
    public function test_post_type_constant() {
        $this->assertEquals( 'hvac_announcement', HVAC_Announcements_CPT::POST_TYPE );
        $this->assertEquals( HVAC_Announcements_CPT::POST_TYPE, HVAC_Announcements_CPT::get_post_type() );
    }
    
    /**
     * Test category taxonomy is registered
     */
    public function test_category_taxonomy_registered() {
        $taxonomy = HVAC_Announcements_CPT::get_category_taxonomy();
        $this->assertTrue( taxonomy_exists( $taxonomy ) );
    }
    
    /**
     * Test tag taxonomy is registered
     */
    public function test_tag_taxonomy_registered() {
        $taxonomy = HVAC_Announcements_CPT::get_tag_taxonomy();
        $this->assertTrue( taxonomy_exists( $taxonomy ) );
    }
    
    /**
     * Test taxonomy constants
     */
    public function test_taxonomy_constants() {
        $this->assertEquals( 'hvac_announcement_cat', HVAC_Announcements_CPT::TAXONOMY_CATEGORY );
        $this->assertEquals( 'hvac_announcement_tag', HVAC_Announcements_CPT::TAXONOMY_TAG );
    }
    
    /**
     * Test post type supports
     */
    public function test_post_type_supports() {
        $post_type = HVAC_Announcements_CPT::get_post_type();
        
        $this->assertTrue( post_type_supports( $post_type, 'title' ) );
        $this->assertTrue( post_type_supports( $post_type, 'editor' ) );
        $this->assertTrue( post_type_supports( $post_type, 'author' ) );
        $this->assertTrue( post_type_supports( $post_type, 'thumbnail' ) );
        $this->assertTrue( post_type_supports( $post_type, 'excerpt' ) );
        $this->assertTrue( post_type_supports( $post_type, 'custom-fields' ) );
    }
    
    /**
     * Test creating an announcement post
     */
    public function test_create_announcement() {
        $post_id = wp_insert_post( array(
            'post_type' => HVAC_Announcements_CPT::get_post_type(),
            'post_title' => 'Test Announcement',
            'post_content' => 'This is a test announcement.',
            'post_status' => 'publish',
        ) );
        
        $this->assertNotEquals( 0, $post_id );
        $this->assertFalse( is_wp_error( $post_id ) );
        
        $post = get_post( $post_id );
        $this->assertEquals( HVAC_Announcements_CPT::get_post_type(), $post->post_type );
        $this->assertEquals( 'Test Announcement', $post->post_title );
        
        // Clean up
        wp_delete_post( $post_id, true );
    }
    
    /**
     * Test assigning categories to announcement
     */
    public function test_assign_categories() {
        // Create announcement
        $post_id = wp_insert_post( array(
            'post_type' => HVAC_Announcements_CPT::get_post_type(),
            'post_title' => 'Test Announcement',
            'post_status' => 'publish',
        ) );
        
        // Create category
        $term = wp_insert_term( 'Test Category', HVAC_Announcements_CPT::get_category_taxonomy() );
        $this->assertFalse( is_wp_error( $term ) );
        
        // Assign category
        $result = wp_set_post_terms( $post_id, array( $term['term_id'] ), HVAC_Announcements_CPT::get_category_taxonomy() );
        $this->assertFalse( is_wp_error( $result ) );
        
        // Verify assignment
        $categories = wp_get_post_terms( $post_id, HVAC_Announcements_CPT::get_category_taxonomy() );
        $this->assertCount( 1, $categories );
        $this->assertEquals( 'Test Category', $categories[0]->name );
        
        // Clean up
        wp_delete_post( $post_id, true );
        wp_delete_term( $term['term_id'], HVAC_Announcements_CPT::get_category_taxonomy() );
    }
    
    /**
     * Test assigning tags to announcement
     */
    public function test_assign_tags() {
        // Create announcement
        $post_id = wp_insert_post( array(
            'post_type' => HVAC_Announcements_CPT::get_post_type(),
            'post_title' => 'Test Announcement',
            'post_status' => 'publish',
        ) );
        
        // Assign tags
        $result = wp_set_post_terms( $post_id, array( 'tag1', 'tag2', 'tag3' ), HVAC_Announcements_CPT::get_tag_taxonomy() );
        $this->assertFalse( is_wp_error( $result ) );
        
        // Verify assignment
        $tags = wp_get_post_terms( $post_id, HVAC_Announcements_CPT::get_tag_taxonomy() );
        $this->assertCount( 3, $tags );
        
        $tag_names = wp_list_pluck( $tags, 'name' );
        $this->assertContains( 'tag1', $tag_names );
        $this->assertContains( 'tag2', $tag_names );
        $this->assertContains( 'tag3', $tag_names );
        
        // Clean up
        wp_delete_post( $post_id, true );
        foreach ( $tags as $tag ) {
            wp_delete_term( $tag->term_id, HVAC_Announcements_CPT::get_tag_taxonomy() );
        }
    }
    
    /**
     * Test post type is not publicly queryable
     */
    public function test_post_type_not_public() {
        $post_type_obj = get_post_type_object( HVAC_Announcements_CPT::get_post_type() );
        
        $this->assertFalse( $post_type_obj->public );
        $this->assertFalse( $post_type_obj->publicly_queryable );
        $this->assertFalse( $post_type_obj->has_archive );
    }
    
    /**
     * Test post type capabilities
     */
    public function test_post_type_capabilities() {
        $post_type_obj = get_post_type_object( HVAC_Announcements_CPT::get_post_type() );
        
        $this->assertEquals( 'hvac_announcement', $post_type_obj->capability_type[0] );
        $this->assertEquals( 'hvac_announcements', $post_type_obj->capability_type[1] );
        $this->assertTrue( $post_type_obj->map_meta_cap );
    }
    
    /**
     * Test singleton pattern
     */
    public function test_singleton_pattern() {
        $instance1 = HVAC_Announcements_CPT::get_instance();
        $instance2 = HVAC_Announcements_CPT::get_instance();
        
        $this->assertSame( $instance1, $instance2 );
    }
}