You are here

public function JsonApiExtrasFunctionalTest::testSortOverwrittenField in JSON:API Extras 8.3

File

tests/src/Functional/JsonApiExtrasFunctionalTest.php, line 161

Class

JsonApiExtrasFunctionalTest
The test class for the main functionality.

Namespace

Drupal\Tests\jsonapi_extras\Functional

Code

public function testSortOverwrittenField() {
  $this
    ->createDefaultContent(2, 1, FALSE, TRUE, static::IS_NOT_MULTILINGUAL);
  $this->nodes[0]->field_text_moved
    ->setValue('c');
  $this->nodes[0]->field_text_moved_new
    ->setValue('b');
  $this->nodes[0]
    ->save();
  $this->nodes[1]->field_text_moved
    ->setValue('d');
  $this->nodes[1]->field_text_moved_new
    ->setValue('a');
  $this->nodes[1]
    ->save();
  $stringResponse = $this
    ->drupalGet('/api/articles', [
    'query' => [
      'sort' => 'field_text_moved.value',
    ],
  ]);
  $output = Json::decode($stringResponse);

  // Check if order changed as expected.
  $this
    ->assertEquals('a', $output['data'][0]['attributes']['field_text_moved']['value']);
  $this
    ->assertEquals('b', $output['data'][1]['attributes']['field_text_moved']['value']);
  $stringResponse = $this
    ->drupalGet('/api/articles', [
    'query' => [
      'sort' => '-field_text_moved.value',
    ],
  ]);
  $output = Json::decode($stringResponse);

  // Check if order changed as expected.
  $this
    ->assertEquals('b', $output['data'][0]['attributes']['field_text_moved']['value']);
  $this
    ->assertEquals('a', $output['data'][1]['attributes']['field_text_moved']['value']);
}