Passed
Branch feature/linting (138974)
by Christopher
02:18
created

drupal_static()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\Tests\paragraphs_editor\Unit\Plugin\dom_processor\data_processor {
4
5
use Drupal\Core\Entity\EntityViewBuilderInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Entity\EntityViewBuilderInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Field\Entity...eFieldItemListInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Drupal\Core\Field\FieldConfigInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Field\FieldConfigInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Drupal\Core\Render\RendererInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Render\RendererInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Drupal\Tests\UnitTestCase;
0 ignored issues
show
Bug introduced by
The type Drupal\Tests\UnitTestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Drupal\Tests\dom_processor\Traits\DomProcessorTestTrait;
0 ignored issues
show
Bug introduced by
The type Drupal\Tests\dom_process...s\DomProcessorTestTrait was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Drupal\Tests\paragraphs_editor\Traits\MockFieldValueManagerTrait;
12
use Drupal\paragraphs\ParagraphInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\paragraphs\ParagraphInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Drupal\paragraphs_editor\EditorFieldValue\FieldValueWrapperInterface;
14
use Drupal\paragraphs_editor\Plugin\dom_processor\data_processor\ParagraphsEditorRenderer;
15
16
/**
17
 * @coversDefaultClass \Drupal\paragraphs_editor\Plugin\dom_processor\data_processor\ParagraphsEditorRenderer
18
 * @group paragraphs_editor
19
 */
20
class ParagraphsEditorRendererUnitTest extends UnitTestCase {
21
  use MockFieldValueManagerTrait;
22
  use DomProcessorTestTrait;
23
24 1
  public function testProcessPassThrough() {
25 1
    $entity_view_builder = $this->prophesize(EntityViewBuilderInterface::CLASS)->reveal();
26 1
    $renderer = $this->prophesize(RendererInterface::CLASS)->reveal();
27 1
    $field_value_manager = $this->createFieldValueManagerProphecy()->reveal();
28 1
    $processor = new ParagraphsEditorRenderer($field_value_manager, $entity_view_builder, $renderer);
29 1
    $data = $this->createDomProcessorData('<div></div>', 'div');
30 1
    $result = $this->createDomProcessorResult();
31 1
    $processor->process($data, $result);
32 1
    $data = $this->createDomProcessorData('<widget></widget>', 'widget');
33 1
    $result = $this->createDomProcessorResult();
34 1
    $processor->process($data, $result);
35 1
    $this->assertTrue(true);
36 1
  }
37
38
  public function testProcess() {
39
    $languages = ['en', 'de'];
40
41
    foreach ($languages as $langcode) {
42
      $view_builder_prophecy = $this->prophesize(EntityViewBuilderInterface::CLASS);
43
      $renderer_prophecy = $this->prophesize(RendererInterface::CLASS);
44
      $manager_prophecy = $this->createFieldValueManagerProphecy();
45
46
      $entities = [];
47
48
      $deeply_nested_prophecy = $this->createChildParagraphProphecy('uuid3', 3);
49
      $deeply_nested_field_definition = $this->createChildFieldDefinitionProphecy()->reveal();
50
      $manager_prophecy->isParagraphsField($deeply_nested_field_definition)->willReturn(TRUE);
51
      $manager_prophecy->isParagraphsEditorField($deeply_nested_field_definition)->willReturn(FALSE);
52
      $prophecy = $this->createChildFieldItemsProphecy();
53
      $prophecy->getFieldDefinition()->willReturn($deeply_nested_field_definition);
54
      $prophecy->referencedEntities()->willReturn([]);
55
      $deeply_nested_items = $prophecy->reveal();
0 ignored issues
show
Unused Code introduced by
The assignment to $deeply_nested_items is dead and can be removed.
Loading history...
56
      $deeply_nested_items = $prophecy->reveal();
57
      $deeply_nested_prophecy->getFields()->willReturn([$deeply_nested_items]);
58
      $deeply_nested_entity = $deeply_nested_prophecy->reveal();
59
60
      $nested_prophecy = $this->createChildParagraphProphecy('uuid2', 2);
61
      $nested_field_definition = $this->createChildFieldDefinitionProphecy()->reveal();
62
      $manager_prophecy->isParagraphsField($nested_field_definition)->willReturn(TRUE);
63
      $manager_prophecy->isParagraphsEditorField($nested_field_definition)->willReturn(FALSE);
64
      $prophecy = $this->createChildFieldItemsProphecy();
65
      $prophecy->getFieldDefinition()->willReturn($nested_field_definition);
66
      $prophecy->referencedEntities()->willReturn([$deeply_nested_entity]);
67
      $nested_items = $prophecy->reveal();
68
      $nested_prophecy->getFields()->willReturn([$nested_items]);
69
      $nested_entity = $nested_prophecy->reveal();
70
71
      $child_prophecy = $this->createChildParagraphProphecy('uuid1', 1);
72
      $child_field_definition = $this->createChildFieldDefinitionProphecy()->reveal();
73
      $prophecy = $this->createChildFieldItemsProphecy();
74
      $prophecy->getFieldDefinition()->willReturn($child_field_definition);
75
      $child_items = $prophecy->reveal();
76
      $child_prophecy->getFields()->willReturn([$child_items]);
77
      $child_entity = $child_prophecy->reveal();
78
      $prophecy = $this->prophesize(FieldValueWrapperInterface::CLASS);
0 ignored issues
show
Bug introduced by
The constant Drupal\paragraphs_editor...WrapperInterface::CLASS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
79
      $prophecy->getReferencedEntities()->willReturn(['uuid2' => $nested_entity]);
80
      $wrapper = $prophecy->reveal();
81
      $manager_prophecy->isParagraphsField($child_field_definition)->willReturn(TRUE);
82
      $manager_prophecy->isParagraphsEditorField($child_field_definition)->willReturn(TRUE);
83
      $manager_prophecy->wrapItems($child_items)->willReturn($wrapper);
84
85
      $view_mode = 'test';
86
      $entities[] = $child_entity;
87
      $entities[] = $nested_entity;
88
      foreach ($entities as $entity) {
89
        $view = [
90
          'uuid' => $entity->uuid(),
91
          'view_mode' => $view_mode,
92
          'langcode' => $langcode,
93
        ];
94
        $view_builder_prophecy->view($entity, $view_mode, $langcode)
95
          ->willReturn($view)
96
          ->shouldBeCalledTimes(1);
97
        $renderer_prophecy->render($view)
98
          ->willReturn(TRUE)
99
          ->shouldBeCalledTimes(1);
100
      }
101
102
      $field_value_manager = $manager_prophecy->reveal();
103
      $entity_view_builder = $view_builder_prophecy->reveal();
104
      $renderer = $renderer_prophecy->reveal();
105
      $processor = new ParagraphsEditorRenderer($field_value_manager, $entity_view_builder, $renderer);
106
      $data = $this->createDomProcessorData('<widget></widget>', 'widget', [
107
        'context_id' => 'test_context',
108
        'paragraph' => [
109
          'entity' => $child_entity,
110
        ],
111
        'langcode' => $langcode,
112
        'settings' => [
113
          'view_mode' => $view_mode,
114
        ],
115
      ]);
116
      $result = $this->createDomProcessorResult();
117
      $processor->process($data, $result);
118
119
      $processor = new ParagraphsEditorRenderer($field_value_manager, $entity_view_builder, $renderer);
120
      $data = $this->createDomProcessorData('<widget></widget>', 'widget', [
121
        'context_id' => 'test_context',
122
        'paragraph' => [
123
          'entity' => $child_entity,
124
        ],
125
        'langcode' => $langcode,
126
        'settings' => [
127
          'view_mode' => $view_mode,
128
        ],
129
      ]);
130
      $result = $this->createDomProcessorResult();
131
      $processor->process($data, $result);
132
      $this->assertTrue(true);
133
    }
134
  }
135
136 1 View Code Duplication
    protected function createFieldValueManagerProphecy() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
137 1
    return $this->createFieldValueManager([
138 1
      'elements' => [
139
        'widget' => [
140
          'tag' => 'widget',
141
          'attributes' => [
142
            'data-context' => '<context>',
143
            'data-uuid' => '<uuid>',
144
          ],
145
          'selector' => 'widget',
146
        ],
147
        'field' => [
148
          'tag' => 'field',
149
          'attributes' => [
150
            'data-name' => '<name>',
151
            'data-editable' => '<editable>',
152
            'data-context' => '<context>',
153
          ],
154
          'selector' => 'field',
155
        ],
156
      ],
157 1
    ], TRUE);
158
  }
159
160 View Code Duplication
  protected function createChildFieldDefinitionProphecy() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
161
    $prophecy = $this->prophesize(FieldConfigInterface::CLASS);
162
    $prophecy->getName()->willReturn('field_test');
163
    $prophecy->id()->willReturn('paragraph.field_test');
164
    return $prophecy;
165
  }
166
167
  protected function createChildFieldItemsProphecy() {
168
    $prophecy = $this->prophesize(EntityReferenceFieldItemListInterface::CLASS);
169
    return $prophecy;
170
  }
171
172
  protected function createChildParagraphProphecy($uuid, $vid) {
173
    $prophecy = $this->prophesize(ParagraphInterface::CLASS);
174
    $prophecy->uuid()->willReturn($uuid);
175
    $prophecy->getRevisionId()->willReturn($vid);
176
    return $prophecy;
177
  }
178
179
}
180
}
181
182
namespace {
183
  if (!function_exists('drupal_static')) {
184
    function &drupal_static() {
185
      static $cache = [];
186
      return $cache;
187
    }
188
  }
189
}
190