<?php

declare(strict_types=1);

/*
 * This file is part of the Ferienpass package.
 *
 * (c) Richard Henkenjohann <richard@ferienpass.online>
 *
 * For more information visit the project website <https://ferienpass.online>
 * or the documentation under <https://docs.ferienpass.online>.
 */

namespace Ferienpass\CmsBundle\Tests\Voter;

use Contao\CoreBundle\Security\ContaoCorePermissions;
use Ferienpass\CmsBundle\Voter\ContentElementsVoter;
use Ferienpass\CmsBundle\Voter\FormFieldsVoter;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\Authentication\Token\NullToken;

final class VotersTest extends TestCase
{
    public function testContentElementsVoterNeverSupports(): void
    {
        $voter = new ContentElementsVoter();

        $ref = new \ReflectionMethod($voter, 'supports');
        self::assertFalse($ref->invoke($voter, 'any_attribute', 'any_subject'));
    }

    public function testContentElementsVoterVoteOnAttributeReturnsFalse(): void
    {
        $voter = new ContentElementsVoter();
        $token = new NullToken();

        $ref = new \ReflectionMethod($voter, 'voteOnAttribute');
        self::assertFalse($ref->invoke($voter, 'attr', null, $token));
    }

    public function testFormFieldsVoterSupportsKnownFieldTypes(): void
    {
        $voter = new FormFieldsVoter();
        $ref = new \ReflectionMethod($voter, 'supports');

        foreach (['html', 'fieldsetStart', 'fieldsetStop', 'password', 'range', 'hidden'] as $type) {
            self::assertTrue(
                $ref->invoke($voter, ContaoCorePermissions::USER_CAN_ACCESS_FIELD_TYPE, $type),
                "Expected supports to return true for '$type'"
            );
        }
    }

    public function testFormFieldsVoterDoesNotSupportUnknownFieldType(): void
    {
        $voter = new FormFieldsVoter();
        $ref = new \ReflectionMethod($voter, 'supports');

        self::assertFalse($ref->invoke($voter, ContaoCorePermissions::USER_CAN_ACCESS_FIELD_TYPE, 'text'));
    }

    public function testFormFieldsVoterDoesNotSupportWrongAttribute(): void
    {
        $voter = new FormFieldsVoter();
        $ref = new \ReflectionMethod($voter, 'supports');

        self::assertFalse($ref->invoke($voter, 'wrong_attribute', 'html'));
    }

    public function testFormFieldsVoterVoteOnAttributeReturnsFalse(): void
    {
        $voter = new FormFieldsVoter();
        $token = new NullToken();

        $ref = new \ReflectionMethod($voter, 'voteOnAttribute');
        self::assertFalse($ref->invoke($voter, 'attr', null, $token));
    }
}
