You are here

class AjaxControllerTest in Sortableviews 8

@coversDefaultClass \Drupal\sortableviews\Controller\AjaxController @group sortableviews

Hierarchy

Expanded class hierarchy of AjaxControllerTest

File

tests/src/Unit/Controller/AjaxControllerTest.php, line 15

Namespace

Drupal\Tests\sortableviews\Unit\Controller
View source
class AjaxControllerTest extends UnitTestCase {

  /**
   * The instance of AjaxController to be tested.
   *
   * @var \Drupal\sortableviews\Controller\AjaxController
   */
  protected $ajaxController;

  /**
   * Instance of \ReflectionMethod for testing of ::retrieveOrderFromRequest().
   *
   * @var \ReflectionMethod
   */
  protected $method;

  /**
   * Initializes container prior to test execution.
   */
  private function initializeContainer() {
    $container = new ContainerBuilder();

    // Mock the translation service.
    $translation_service = $this
      ->createMock('Drupal\\Core\\StringTranslation\\TranslationInterface');
    $container
      ->set('string_translation', $translation_service);

    // Mock the renderer service.
    $renderer = $this
      ->createMock('Drupal\\Core\\Render\\RendererInterface');
    $render = 'something';
    $renderer
      ->expects($this
      ->any())
      ->method('renderRoot')
      ->with()
      ->willReturnCallback(function (&$elements) use ($render) {
      $elements['#attached'] = [];
      return $render;
    });
    $container
      ->set('renderer', $renderer);

    // Create the container.
    \Drupal::setContainer($container);
  }

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    $this
      ->initializeContainer();
    $entity_storage = $this
      ->createMock('Drupal\\Core\\Entity\\EntityStorageInterface');
    $entity_storage
      ->expects($this
      ->any())
      ->method('loadMultiple')
      ->willReturn([]);
    $entity_manager = $this
      ->createMock('Drupal\\Core\\Entity\\EntityTypeManagerInterface');
    $entity_manager
      ->expects($this
      ->any())
      ->method('getStorage')
      ->with('some_type')
      ->willReturn($entity_storage);
    $this->ajaxController = new AjaxController($entity_manager);
    $class = new \ReflectionClass($this->ajaxController);
    $this->method = $class
      ->getMethod('retrieveOrderFromRequest');
    $this->method
      ->setAccessible(TRUE);
  }

  /**
   * Tests the ajaxSave method.
   *
   * @covers ::ajaxSave
   */
  public function testAjaxSave() {
    $dom_id = 'domid';
    $request = new Request();
    $request->attributes
      ->set('entity_type', 'some_type');
    $request->attributes
      ->set('weight_field', 'some_field');
    $request->attributes
      ->set('items_per_page', 2);
    $request->attributes
      ->set('sort_order', 'asc');
    $request->attributes
      ->set('page_number', 0);
    $request->attributes
      ->set('dom_id', $dom_id);
    $request->attributes
      ->set('current_order', []);
    $response = $this->ajaxController
      ->ajaxSave($request);
    $this
      ->assertTrue($response instanceof AjaxResponse);
    $commands = $response
      ->getCommands($response);
    $this
      ->assertEquals('insert', $commands[0]['command']);
    $this
      ->assertEquals('prepend', $commands[0]['method']);
    $this
      ->assertEquals('.js-view-dom-id-' . $dom_id, $commands[0]['selector']);
    $this
      ->assertEquals('remove', $commands[1]['command']);
    $this
      ->assertEquals('.js-view-dom-id-' . $dom_id . ' .sortableviews-ajax-trigger', $commands[1]['selector']);
  }

  /**
   * Tests adjusted order for entities when not in pager.
   *
   * @covers ::retrieveOrderFromRequest
   */
  public function testAnyOrderNotInPager() {

    // Test order when not in a pager.
    $request = new Request();
    $page = [
      2,
      4,
      6,
      8,
    ];
    $request->attributes
      ->set('current_order', $page);
    $result = $this->method
      ->invokeArgs($this->ajaxController, [
      $request,
    ]);
    $this
      ->assertEquals($page, $result);
  }

  /**
   * Tests adjusted order for entities when in ASC pager.
   *
   * Uses an imaginary view with 3 pages, 3 items per page
   * and 8 rows.
   *
   * @covers ::retrieveOrderFromRequest
   */
  public function testAscOrderInPager() {
    $request = new Request();
    $request->attributes
      ->set('items_per_page', 3);
    $request->attributes
      ->set('sort_order', 'asc');

    // Test page one.
    $page = [
      1,
      2,
      3,
    ];
    $request->attributes
      ->set('page_number', 0);
    $request->attributes
      ->set('current_order', $page);
    $result = $this->method
      ->invokeArgs($this->ajaxController, [
      $request,
    ]);
    $this
      ->assertEquals($page, $result);

    // Test page two.
    $page = [
      5,
      6,
      4,
    ];
    $request->attributes
      ->set('page_number', 1);
    $request->attributes
      ->set('current_order', $page);
    $result = $this->method
      ->invokeArgs($this->ajaxController, [
      $request,
    ]);
    $this
      ->assertEquals([
      3 => 5,
      4 => 6,
      5 => 4,
    ], $result);

    // Test page three.
    $page = [
      8,
      7,
    ];
    $request->attributes
      ->set('page_number', 2);
    $request->attributes
      ->set('current_order', $page);
    $result = $this->method
      ->invokeArgs($this->ajaxController, [
      $request,
    ]);
    $this
      ->assertEquals([
      6 => 8,
      7 => 7,
    ], $result);
  }

  /**
   * Tests adjusted order for entities when in DESC pager.
   *
   * Uses an imaginary view with 3 pages, 3 items per page
   * and 7 rows.
   *
   * @covers ::retrieveOrderFromRequest
   */
  public function testDescOrderInPager() {
    $request = new Request();
    $request->attributes
      ->set('items_per_page', 3);
    $request->attributes
      ->set('sort_order', 'desc');
    $request->attributes
      ->set('total_rows', 7);

    // Test page one.
    $page = [
      6,
      7,
      5,
    ];
    $request->attributes
      ->set('page_number', 0);
    $request->attributes
      ->set('current_order', $page);
    $result = $this->method
      ->invokeArgs($this->ajaxController, [
      $request,
    ]);
    $this
      ->assertEquals([
      4 => 6,
      5 => 7,
      6 => 5,
    ], $result);

    // Test page two.
    $page = [
      4,
      2,
      3,
    ];
    $request->attributes
      ->set('page_number', 1);
    $request->attributes
      ->set('current_order', $page);
    $result = $this->method
      ->invokeArgs($this->ajaxController, [
      $request,
    ]);
    $this
      ->assertEquals([
      1 => 4,
      2 => 2,
      3 => 3,
    ], $result);

    // Test page three.
    $page = [
      1,
    ];
    $request->attributes
      ->set('page_number', 2);
    $request->attributes
      ->set('current_order', $page);
    $result = $this->method
      ->invokeArgs($this->ajaxController, [
      $request,
    ]);
    $this
      ->assertEquals($page, $result);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AjaxControllerTest::$ajaxController protected property The instance of AjaxController to be tested.
AjaxControllerTest::$method protected property Instance of \ReflectionMethod for testing of ::retrieveOrderFromRequest().
AjaxControllerTest::initializeContainer private function Initializes container prior to test execution.
AjaxControllerTest::setUp protected function Overrides UnitTestCase::setUp
AjaxControllerTest::testAjaxSave public function Tests the ajaxSave method.
AjaxControllerTest::testAnyOrderNotInPager public function Tests adjusted order for entities when not in pager.
AjaxControllerTest::testAscOrderInPager public function Tests adjusted order for entities when in ASC pager.
AjaxControllerTest::testDescOrderInPager public function Tests adjusted order for entities when in DESC pager.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.