class AjaxControllerTest in Sortableviews 8
@coversDefaultClass \Drupal\sortableviews\Controller\AjaxController @group sortableviews
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\sortableviews\Unit\Controller\AjaxControllerTest
Expanded class hierarchy of AjaxControllerTest
File
- tests/
src/ Unit/ Controller/ AjaxControllerTest.php, line 15
Namespace
Drupal\Tests\sortableviews\Unit\ControllerView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AjaxControllerTest:: |
protected | property | The instance of AjaxController to be tested. | |
AjaxControllerTest:: |
protected | property | Instance of \ReflectionMethod for testing of ::retrieveOrderFromRequest(). | |
AjaxControllerTest:: |
private | function | Initializes container prior to test execution. | |
AjaxControllerTest:: |
protected | function |
Overrides UnitTestCase:: |
|
AjaxControllerTest:: |
public | function | Tests the ajaxSave method. | |
AjaxControllerTest:: |
public | function | Tests adjusted order for entities when not in pager. | |
AjaxControllerTest:: |
public | function | Tests adjusted order for entities when in ASC pager. | |
AjaxControllerTest:: |
public | function | Tests adjusted order for entities when in DESC pager. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |