You are here

MixitUpFiltersFormTest.php in MixItUp Views 8.2

Same filename and directory in other branches
  1. 8 tests/src/Kernel/MixitUpFiltersFormTest.php

File

tests/src/Kernel/MixitUpFiltersFormTest.php
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.
 *
 * @group MixItUp Views
 * @package Drupal\Tests\mixitup_views\Kernel
 */
class MixitUpFiltersFormTest extends KernelTestBase {
  use TestContentGenerationTrait;

  /**
   * Class to be tested.
   *
   * @var \Drupal\mixitup_views\MixitupFunc
   */
  protected $testObject;

  /**
   * EntityTypeManager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManager
   */
  protected $entityTypeManager;

  /**
   * Default options service.
   *
   * @var \Drupal\mixitup_views\MixitupViewsDefaultOptionsService
   */
  protected $defaultOptions;

  /**
   * MixItUpFunc service.
   *
   * @var \Drupal\mixitup_views\MixitupFunc
   */
  protected $mixitupFunc;

  /**
   * The service container.
   *
   * @var \Symfony\Component\DependencyInjection\ContainerInterface
   */
  protected $serviceContainer;

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = [
    'system',
    'field',
    'text',
    'entity_reference',
    'user',
    'node',
    'taxonomy',
    'mixitup_views',
  ];

  /**
   * Before a test method is run, setUp() is invoked.
   *
   * Create new unit object.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  protected function setUp() {
    parent::setUp();

    // Installing entities schema.
    $this
      ->installSchema('system', [
      'sequences',
      'key_value_expire',
    ]);
    $this
      ->installEntitySchema('user');
    $this
      ->installEntitySchema('taxonomy_term');
    $this
      ->installEntitySchema('taxonomy_vocabulary');
    $this
      ->installEntitySchema('node');

    // Generating test content and creating instance for the class to be tested.
    $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();
  }

  /**
   * Covers buildForm method in MixitupFiltersForm class.
   *
   * @covers \Drupal\mixitup_views\Form\MixitupFiltersForm::buildForm
   */
  public function testBuildForm() {

    // Creating MixItUpFunc Service instance with one of nodes.
    $this->mixitupFunc
      ->getRowClasses(5);

    // Defining options array.
    $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,
    ];

    // Tests build form.
    $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']);

    // Creating MixItUpFunc Service instance with one of nodes.
    $this->mixitupFunc
      ->getRowClasses(4);

    // Tests build form.
    $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']);

    // Creating MixItUpFunc Service instance with one of nodes.
    $this->mixitupFunc
      ->getRowClasses(3);

    // Tests build form.
    $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']);

    // Creating MixItUpFunc Service instance with one of nodes.
    $this->mixitupFunc
      ->getRowClasses(2);

    // Tests build form.
    $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']);

    // Creating MixItUpFunc Service instance with one of nodes.
    $this->mixitupFunc
      ->getRowClasses(1);

    // Tests build form.
    $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']);
  }

  /**
   * If test has finished running, tearDown() will be invoked.
   *
   * Unset the $testObject object.
   */
  public function tearDown() {
    unset($this->testObject);
  }

}

Classes

Namesort descending Description
MixitUpFiltersFormTest Class MixitUpFiltersFormTest.