View source  
  <?php
namespace Drupal\Tests\mixitup_views\Kernel;
use Drupal\Core\Form\FormState;
use Drupal\KernelTests\KernelTestBase;
use Drupal\mixitup_views\Form\MixitupFiltersForm;
class MixitUpFiltersFormTest extends KernelTestBase {
  use TestContentGenerationTrait;
  
  protected $testObject;
  
  protected $entityTypeManager;
  
  protected $defaultOptions;
  
  protected $mixitupFunc;
  
  protected $serviceContainer;
  
  public static $modules = [
    'system',
    'field',
    'text',
    'entity_reference',
    'user',
    'node',
    'taxonomy',
    'mixitup_views',
  ];
  
  protected function setUp() {
    parent::setUp();
    
    $this
      ->installSchema('system', [
      'sequences',
      'key_value_expire',
    ]);
    $this
      ->installEntitySchema('user');
    $this
      ->installEntitySchema('taxonomy_term');
    $this
      ->installEntitySchema('taxonomy_vocabulary');
    $this
      ->installEntitySchema('node');
    
    $this->serviceContainer = \Drupal::getContainer();
    $this->entityTypeManager = $this->serviceContainer
      ->get('entity_type.manager');
    $this->defaultOptions = $this->serviceContainer
      ->get('mixitup_views.default_options_service');
    $this->mixitupFunc = $this->serviceContainer
      ->get('mixitup_views.func_service');
    $this->testObject = new MixitupFiltersForm($this->mixitupFunc);
    $this
      ->generateTestContent();
  }
  
  public function testBuildForm() {
    
    $this->mixitupFunc
      ->getRowClasses(5);
    
    $options = [
      'grouping' => [],
      'row_class' => '',
      'default_row_class' => TRUE,
      'uses_fields' => FALSE,
      'selectors_target' => '.mix',
      'selectors_filter' => '.filter',
      'selectors_sort' => '.sort',
      'load_filter' => 'all',
      'load_sort' => 'default:asc',
      'animation_enable' => 1,
      'animation_effects' => 'fade scale',
      'animation_duration' => '600',
      'animation_easing' => 'ease',
      'animation_perspectiveDistance' => '3000px',
      'animation_perspectiveOrigin' => '50% 50%',
      'animation_queue' => 1,
      'animation_queueLimit' => '1',
      'restrict_vocab' => 0,
      'restrict_vocab_ids' => [],
      'filter_type' => 'checkboxes',
      'agregation_type' => 'or',
      'use_sort' => 0,
      'sorts' => [
        'node_field_data_created' => 'Created',
      ],
      'hide_unchecked_chekboxes' => 0,
    ];
    
    $form = [];
    $form_state = new FormState();
    $form = $this->testObject
      ->buildForm($form, $form_state, $options);
    self::assertEquals('<a href="#reset" id="reset">Reset filters</a>', $form['reset']['#markup']);
    self::assertEquals('checkboxes', $form['filter_tags']['#type']);
    $filter_options = [
      '.tid_2' => 'test name1',
      '.tid_3' => 'test name2',
    ];
    self::assertEquals($filter_options, $form['filter_tags']['#options']);
    self::assertEquals('tags', $form['filter_tags']['#attributes']['vid']);
    
    $this->mixitupFunc
      ->getRowClasses(4);
    
    $form = [];
    $form_state = new FormState();
    $form = $this->testObject
      ->buildForm($form, $form_state, $options);
    $filter_options = [
      '.tid_1' => 'test name0',
      '.tid_4' => 'test name3',
      '.tid_2' => 'test name1',
      '.tid_3' => 'test name2',
    ];
    self::assertEquals($filter_options, $form['filter_tags']['#options']);
    
    $this->mixitupFunc
      ->getRowClasses(3);
    
    $form = [];
    $form_state = new FormState();
    $form = $this->testObject
      ->buildForm($form, $form_state, $options);
    unset($filter_options);
    $filter_options = [
      '.tid_1' => 'test name0',
      '.tid_5' => 'test name4',
      '.tid_4' => 'test name3',
      '.tid_2' => 'test name1',
      '.tid_3' => 'test name2',
    ];
    self::assertEquals($filter_options, $form['filter_tags']['#options']);
    
    $this->mixitupFunc
      ->getRowClasses(2);
    
    $form = [];
    $form_state = new FormState();
    $form = $this->testObject
      ->buildForm($form, $form_state, $options);
    unset($filter_options);
    $filter_options = [
      '.tid_3' => 'test name2',
      '.tid_4' => 'test name3',
      '.tid_1' => 'test name0',
      '.tid_5' => 'test name4',
      '.tid_2' => 'test name1',
    ];
    self::assertEquals($filter_options, $form['filter_tags']['#options']);
    
    $this->mixitupFunc
      ->getRowClasses(1);
    
    $form = [];
    $form_state = new FormState();
    $form = $this->testObject
      ->buildForm($form, $form_state, $options);
    unset($filter_options);
    $filter_options = [
      '.tid_2' => 'test name1',
      '.tid_4' => 'test name3',
      '.tid_3' => 'test name2',
      '.tid_1' => 'test name0',
      '.tid_5' => 'test name4',
    ];
    self::assertEquals($filter_options, $form['filter_tags']['#options']);
  }
  
  public function tearDown() {
    unset($this->testObject);
  }
}