Completed
Pull Request — 8.x-3.x (#501)
by Philipp
04:28
created

EntityByIdTest::testEntityByIdWithTranslation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 56
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 37
nc 1
nop 0
dl 0
loc 56
rs 9.7251
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Drupal\Tests\graphql_core\Kernel\Entity;
4
5
use Drupal\Tests\graphql_core\Kernel\GraphQLContentTestBase;
6
7
/**
8
 * Test entity query support in GraphQL.
9
 *
10
 * @group graphql_content
11
 */
12
class EntityByIdTest extends GraphQLContentTestBase {
13
14
  /**
15
   * {@inheritdoc}
16
   */
17
  public static $modules = [
18
    'language',
19
    'content_translation',
20
  ];
21
22
  /**
23
   * The added French language.
24
   *
25
   * @var string
26
   */
27
  protected $frenchLangcode = 'fr';
28
29
  /**
30
   * The added Chinese simplified language.
31
   *
32
   * @var string
33
   */
34
  protected $chineseSimplifiedLangcode = 'zh-hans';
35
36
  /**
37
   * {@inheritdoc}
38
   */
39
  protected function setUp() {
40
    parent::setUp();
41
42
    $languageStorage = $this->container->get('entity.manager')->getStorage('configurable_language');
43
    $language = $languageStorage->create([
44
      'id' => $this->frenchLangcode,
45
    ]);
46
    $language->save();
47
48
    $language = $languageStorage->create([
49
      'id' => $this->chineseSimplifiedLangcode,
50
    ]);
51
    $language->save();
52
  }
53
54
  /**
55
   * Test that the entity query returns all nodes if no args are given.
56
   */
57
  public function testEntityByIdWithTranslation() {
58
    $node = $this->createNode([
59
      'title' => 'English node',
60
      'type' => 'test',
61
    ]);
62
    $node->save();
63
    $node->addTranslation($this->frenchLangcode, ['title' => 'French node'])->save();
64
    $node->addTranslation($this->chineseSimplifiedLangcode, ['title' => 'Chinese simplified node'])->save();
65
66
    // Save a new draft.
67
    $this
68
      ->getNewDraft($node)
69
      ->setPublished(FALSE)
70
      ->setTitle('English node unpublished')
71
      ->save();
72
73
    // TODO: Check chache metadata.
74
    $metadata = $this->defaultCacheMetaData();
75
    $metadata->addCacheTags([
76
      'entity_bundles',
77
      'entity_field_info',
78
      'entity_types',
79
      'node:1',
80
      'config:field.storage.node.body',
81
    ]);
82
83
    // Check English node.
84
    $this->assertResults($this->getQueryFromFile('entity_by_id.gql'), [
85
      'id' => $node->id(),
86
      'language' => 'en',
87
    ], [
88
      'nodeById' => [
89
        'entityLabel' => 'English node',
90
      ],
91
    ], $metadata);
92
93
    // Check French translation.
94
    $this->assertResults($this->getQueryFromFile('entity_by_id.gql'), [
95
      'id' => $node->id(),
96
      'language' => 'fr',
97
    ], [
98
      'nodeById' => [
99
        'entityLabel' => 'French node',
100
      ],
101
    ], $metadata);
102
103
    // Check Chinese simplified translation.
104
    $this->assertResults($this->getQueryFromFile('entity_by_id.gql'), [
105
      'id' => $node->id(),
106
      'language' => 'zh_hans',
107
    ], [
108
      'nodeById' => [
109
        'entityLabel' => 'Chinese simplified node',
110
      ],
111
    ], $metadata);
112
  }
113
114
}
115