function TermHalJsonAnonTest::getExpectedNormalizedEntity

Same name in other branches
  1. 8.9.x core/modules/taxonomy/tests/src/Functional/Hal/TermHalJsonAnonTest.php \Drupal\Tests\taxonomy\Functional\Hal\TermHalJsonAnonTest::getExpectedNormalizedEntity()

Overrides TermResourceTestBase::getExpectedNormalizedEntity

File

core/modules/hal/tests/src/Functional/taxonomy/TermHalJsonAnonTest.php, line 42

Class

TermHalJsonAnonTest
@group hal @group legacy

Namespace

Drupal\Tests\hal\Functional\taxonomy

Code

protected function getExpectedNormalizedEntity() {
    $default_normalization = parent::getExpectedNormalizedEntity();
    $normalization = $this->applyHalFieldNormalization($default_normalization);
    // We test with multiple parent terms, and combinations thereof.
    // @see ::createEntity()
    // @see ::testGet()
    // @see ::testGetTermWithParent()
    // @see ::providerTestGetTermWithParent()
    // @see ::testGetTermWithParent()
    $parent_term_ids = [];
    for ($i = 0; $i < $this->entity
        ->get('parent')
        ->count(); $i++) {
        $parent_term_ids[$i] = (int) $this->entity
            ->get('parent')[$i]->target_id;
    }
    $expected_parent_normalization_links = FALSE;
    $expected_parent_normalization_embedded = FALSE;
    switch ($parent_term_ids) {
        case [
            0,
        ]:
            $expected_parent_normalization_links = [
                NULL,
            ];
            $expected_parent_normalization_embedded = [
                NULL,
            ];
            break;
        case [
            2,
        ]:
            $expected_parent_normalization_links = [
                [
                    'href' => $this->baseUrl . '/taxonomy/term/2?_format=hal_json',
                ],
            ];
            $expected_parent_normalization_embedded = [
                [
                    '_links' => [
                        'self' => [
                            'href' => $this->baseUrl . '/taxonomy/term/2?_format=hal_json',
                        ],
                        'type' => [
                            'href' => $this->baseUrl . '/rest/type/taxonomy_term/camelids',
                        ],
                    ],
                    'uuid' => [
                        [
                            'value' => Term::load(2)->uuid(),
                        ],
                    ],
                ],
            ];
            break;
        case [
            0,
            2,
        ]:
            $expected_parent_normalization_links = [
                NULL,
                [
                    'href' => $this->baseUrl . '/taxonomy/term/2?_format=hal_json',
                ],
            ];
            $expected_parent_normalization_embedded = [
                NULL,
                [
                    '_links' => [
                        'self' => [
                            'href' => $this->baseUrl . '/taxonomy/term/2?_format=hal_json',
                        ],
                        'type' => [
                            'href' => $this->baseUrl . '/rest/type/taxonomy_term/camelids',
                        ],
                    ],
                    'uuid' => [
                        [
                            'value' => Term::load(2)->uuid(),
                        ],
                    ],
                ],
            ];
            break;
        case [
            3,
            2,
        ]:
            $expected_parent_normalization_links = [
                [
                    'href' => $this->baseUrl . '/taxonomy/term/3?_format=hal_json',
                ],
                [
                    'href' => $this->baseUrl . '/taxonomy/term/2?_format=hal_json',
                ],
            ];
            $expected_parent_normalization_embedded = [
                [
                    '_links' => [
                        'self' => [
                            'href' => $this->baseUrl . '/taxonomy/term/3?_format=hal_json',
                        ],
                        'type' => [
                            'href' => $this->baseUrl . '/rest/type/taxonomy_term/camelids',
                        ],
                    ],
                    'uuid' => [
                        [
                            'value' => Term::load(3)->uuid(),
                        ],
                    ],
                ],
                [
                    '_links' => [
                        'self' => [
                            'href' => $this->baseUrl . '/taxonomy/term/2?_format=hal_json',
                        ],
                        'type' => [
                            'href' => $this->baseUrl . '/rest/type/taxonomy_term/camelids',
                        ],
                    ],
                    'uuid' => [
                        [
                            'value' => Term::load(2)->uuid(),
                        ],
                    ],
                ],
            ];
            break;
    }
    return $normalization + [
        '_links' => [
            'self' => [
                'href' => $this->baseUrl . '/llama?_format=hal_json',
            ],
            'type' => [
                'href' => $this->baseUrl . '/rest/type/taxonomy_term/camelids',
            ],
            $this->baseUrl . '/rest/relation/taxonomy_term/camelids/parent' => $expected_parent_normalization_links,
        ],
        '_embedded' => [
            $this->baseUrl . '/rest/relation/taxonomy_term/camelids/parent' => $expected_parent_normalization_embedded,
        ],
    ];
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.