<?php
/**
 * Unit Tests for HVAC Announcements Permissions
 *
 * @package HVAC_Community_Events
 * @subpackage Tests
 */

class Test_HVAC_Announcements_Permissions extends WP_UnitTestCase {
    
    /**
     * Test users
     */
    private $admin_user;
    private $master_trainer_user;
    private $trainer_user;
    private $subscriber_user;
    
    /**
     * Setup before each test
     */
    public function setUp() {
        parent::setUp();
        
        // Create test users with different roles
        $this->admin_user = $this->factory->user->create( array(
            'role' => 'administrator',
            'user_login' => 'test_admin',
            'user_email' => 'admin@example.com',
        ) );
        
        $this->master_trainer_user = $this->factory->user->create( array(
            'role' => 'hvac_master_trainer',
            'user_login' => 'test_master',
            'user_email' => 'master@example.com',
        ) );
        
        $this->trainer_user = $this->factory->user->create( array(
            'role' => 'hvac_trainer',
            'user_login' => 'test_trainer',
            'user_email' => 'trainer@example.com',
        ) );
        
        $this->subscriber_user = $this->factory->user->create( array(
            'role' => 'subscriber',
            'user_login' => 'test_subscriber',
            'user_email' => 'subscriber@example.com',
        ) );
        
        // Add capabilities
        HVAC_Announcements_Permissions::add_capabilities();
    }
    
    /**
     * Teardown after each test
     */
    public function tearDown() {
        parent::tearDown();
        
        // Clean up test users
        wp_delete_user( $this->admin_user );
        wp_delete_user( $this->master_trainer_user );
        wp_delete_user( $this->trainer_user );
        wp_delete_user( $this->subscriber_user );
        
        // Remove capabilities
        HVAC_Announcements_Permissions::remove_capabilities();
    }
    
    /**
     * Test is_master_trainer method
     */
    public function test_is_master_trainer() {
        // Test admin is considered master trainer
        $this->assertTrue( HVAC_Announcements_Permissions::is_master_trainer( $this->admin_user ) );
        
        // Test master trainer is master trainer
        $this->assertTrue( HVAC_Announcements_Permissions::is_master_trainer( $this->master_trainer_user ) );
        
        // Test regular trainer is not master trainer
        $this->assertFalse( HVAC_Announcements_Permissions::is_master_trainer( $this->trainer_user ) );
        
        // Test subscriber is not master trainer
        $this->assertFalse( HVAC_Announcements_Permissions::is_master_trainer( $this->subscriber_user ) );
    }
    
    /**
     * Test is_trainer method
     */
    public function test_is_trainer() {
        // Test admin is considered trainer
        $this->assertTrue( HVAC_Announcements_Permissions::is_trainer( $this->admin_user ) );
        
        // Test master trainer is trainer
        $this->assertTrue( HVAC_Announcements_Permissions::is_trainer( $this->master_trainer_user ) );
        
        // Test regular trainer is trainer
        $this->assertTrue( HVAC_Announcements_Permissions::is_trainer( $this->trainer_user ) );
        
        // Test subscriber is not trainer
        $this->assertFalse( HVAC_Announcements_Permissions::is_trainer( $this->subscriber_user ) );
    }
    
    /**
     * Test current_user_can_create method
     */
    public function test_current_user_can_create() {
        // Test as admin
        wp_set_current_user( $this->admin_user );
        $this->assertTrue( HVAC_Announcements_Permissions::current_user_can_create() );
        
        // Test as master trainer
        wp_set_current_user( $this->master_trainer_user );
        $this->assertTrue( HVAC_Announcements_Permissions::current_user_can_create() );
        
        // Test as regular trainer
        wp_set_current_user( $this->trainer_user );
        $this->assertFalse( HVAC_Announcements_Permissions::current_user_can_create() );
        
        // Test as subscriber
        wp_set_current_user( $this->subscriber_user );
        $this->assertFalse( HVAC_Announcements_Permissions::current_user_can_create() );
    }
    
    /**
     * Test current_user_can_read method
     */
    public function test_current_user_can_read() {
        // Test as admin
        wp_set_current_user( $this->admin_user );
        $this->assertTrue( HVAC_Announcements_Permissions::current_user_can_read() );
        
        // Test as master trainer
        wp_set_current_user( $this->master_trainer_user );
        $this->assertTrue( HVAC_Announcements_Permissions::current_user_can_read() );
        
        // Test as regular trainer
        wp_set_current_user( $this->trainer_user );
        $this->assertTrue( HVAC_Announcements_Permissions::current_user_can_read() );
        
        // Test as subscriber
        wp_set_current_user( $this->subscriber_user );
        $this->assertFalse( HVAC_Announcements_Permissions::current_user_can_read() );
    }
    
    /**
     * Test get_active_trainers method
     */
    public function test_get_active_trainers() {
        // Get active trainers
        $active_trainers = HVAC_Announcements_Permissions::get_active_trainers();
        
        // Should include master trainer and regular trainer (not admin by default)
        $trainer_ids = wp_list_pluck( $active_trainers, 'ID' );
        $this->assertContains( $this->master_trainer_user, $trainer_ids );
        $this->assertContains( $this->trainer_user, $trainer_ids );
        
        // Test caching
        $cached_trainers = HVAC_Announcements_Permissions::get_active_trainers();
        $this->assertEquals( $active_trainers, $cached_trainers );
    }
    
    /**
     * Test user_should_receive_emails method
     */
    public function test_user_should_receive_emails() {
        // Test valid trainer with email
        $this->assertTrue( HVAC_Announcements_Permissions::user_should_receive_emails( $this->trainer_user ) );
        
        // Test subscriber should not receive emails
        $this->assertFalse( HVAC_Announcements_Permissions::user_should_receive_emails( $this->subscriber_user ) );
        
        // Test disabled user
        update_user_meta( $this->trainer_user, 'account_status', 'disabled' );
        $this->assertFalse( HVAC_Announcements_Permissions::user_should_receive_emails( $this->trainer_user ) );
        delete_user_meta( $this->trainer_user, 'account_status' );
        
        // Test opted-out user
        update_user_meta( $this->trainer_user, 'hvac_announcement_emails_opt_out', 'yes' );
        $this->assertFalse( HVAC_Announcements_Permissions::user_should_receive_emails( $this->trainer_user ) );
        delete_user_meta( $this->trainer_user, 'hvac_announcement_emails_opt_out' );
    }
    
    /**
     * Test cache invalidation
     */
    public function test_cache_invalidation() {
        // Get initial trainers
        $trainers_before = HVAC_Announcements_Permissions::get_active_trainers();
        
        // Create new trainer
        $new_trainer = $this->factory->user->create( array(
            'role' => 'hvac_trainer',
            'user_login' => 'new_trainer',
            'user_email' => 'new@example.com',
        ) );
        
        // Clear cache manually (normally done by hooks)
        $permissions = HVAC_Announcements_Permissions::get_instance();
        $permissions->clear_trainers_cache();
        
        // Get trainers again
        $trainers_after = HVAC_Announcements_Permissions::get_active_trainers();
        
        // Should have one more trainer
        $this->assertEquals( count($trainers_before) + 1, count($trainers_after) );
        
        // Clean up
        wp_delete_user( $new_trainer );
    }
    
    /**
     * Test email validation in user_should_receive_emails
     */
    public function test_email_validation() {
        // Create user with invalid email
        $invalid_email_user = $this->factory->user->create( array(
            'role' => 'hvac_trainer',
            'user_login' => 'invalid_email_user',
            'user_email' => 'not-an-email',
        ) );
        
        // Should return false for invalid email
        $this->assertFalse( HVAC_Announcements_Permissions::user_should_receive_emails( $invalid_email_user ) );
        
        // Clean up
        wp_delete_user( $invalid_email_user );
    }
}