TestContentGenerationTrait   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 157
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A generateTabs() 0 18 2
A generateEditorText() 0 9 1
A generateTab() 0 6 1
1
<?php
2
3
namespace Drupal\Tests\paragraphs_editor\Traits;
4
5
trait TestContentGenerationTrait {
6
7
  /*public function createMockViewBuilder($type) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
8
    $prophecy = $this->prophesize(ViewBuilderInterface::CLASS);
9
    $prophecy->view($type, Argument::any(), Argument::any())->will(function ($args) use ($type) {
10
      return [
11
        'tag' => $type . ':' . $args[1]->uuid(),
12
      ];
13
    });
14
    return $prophecy->reveal();
15
  }
16
17
  protected function createEntityStorage($type) {
18
    $prophecy_factory = $this;
19
20
    if ($type == 'node') {
21
      $prophecy = $this->prophesize(NodeStorage::CLASS);
22
    }
23
    else if ($type == 'field_config') {
24
      $prophecy = $this->prophesize(FieldConfigStorage::CLASS);
25
    }
26
    else if ($type == 'paragraph') {
27
      $prophecy = $this->prophesize(ParagraphStorage::CLASS);
28
    }
29
    else if ($type == 'paragraphs_type') {
30
      $prophecy = $this->prophesize(ParagraphsTypeStorage::CLASS);
31
    }
32
33
    $prophecy->create(Argument::any())->will(function ($args) {
34
    });
35
    $prophecy->load(Argument::any())->will(function ($args) {
36
    });
37
    $prophecy->loadRevision(Argument::any())->will(function ($args) {
38
    });
39
    $prophecy->loadByProperties(Argument::any())->will(function ($args) {
40
    });
41
42
    $cache[$type] = $prophecy->reveal();
43
  }
44
45
  protected function createFieldItems($entity, $field_definition) {
46
    if ($field_definition->getType() == 'entity_reference_revisions') {
47
      $prophecy = $this->prophesize();
48
    }
49
    else {
50
      $prophecy = $this->prophesize();
51
    }
52
    return $prophecy->reveal();
53
  }
54
55
  protected function createEntity($type, $attributes) {
56
    if ($type == 'node') {
57
      $prophecy = $this->prophesize(NodeInterface::CLASS);
58
    }
59
    else if ($type == 'field_config') {
60
      $prophecy = $this->prophesize(FieldConfigInterface::CLASS);
61
    }
62
    else if ($type == 'paragraph') {
63
      $prophecy = $this->prophesize(ParagraphInterface::CLASS);
64
    }
65
    else if ($type == 'paragraphs_type') {
66
      $prophecy = $this->prophesize(ParagraphsTypeInterface::CLASS);
67
    }
68
69
    $prophecy->getEntityTypeId()->willReturn($type);
70
    $prophecy->bundle()->willReturn($attributes['type']);
71
    $entity = $prophecy->reveal();
72
73
    if ($type == 'node' || $type == 'paragraph') {
74
      $field_definitions = $container->get('entity_type.manager.bundles.' . $type);
75
      if ($field_definitions) {
76
        foreach ($field_definitions as $field_definition) {
77
          $entity->{$field_definition->getName()} = $this->createFieldItems($entity, $field_definition);
78
        }
79
      }
80
    }
81
82
    return $entity;
83
  }
84
85
  protected function createAccessControlHandler() {
86
  }
87
88
  protected function getContainer() {
89
    if (isset($this->container)) {
90
      return $this->container;
91
    }
92
93
    $container = new Container();
94
    $prophecy = $this->prophesize(EntityTypeManagerInterface::CLASS);
95
    $prophecy->getStorage(Argument::type('string'))->will(function ($args) use ($container) {
96
      return $container->get('entity_type.manager.storage.' . $args[0]);
97
    });
98
    $prophecy->getAccessControlHandler(Argument::type('string'))->will(function ($args) use ($container) {
99
      return $container->get('entity_type.manager.access.' . $args[0]);
100
    });
101
    $prophecy->getViewBuilder(Argument::type('string'))->will(function ($args) use ($container) {
102
      return $container->get('entity_type.manager.view_builder.' . $args[0]);
103
    });
104
    $container->set('entity_type.manager', $prophecy->reveal());
105
106
    //$container->set('entity_type.manager.access.node', );
107
    //$container->set('entity_type.manager.access.paragraph', []);
108
    $container->set('entity_type.manager.storage.node', $this->createEntityStorage('node'));
109
    $container->set('entity_type.manager.storage.paragraph', $this->createEntityStorage('paragraph'));
110
    $container->set('entity_type.manager.storage.paragraphs_type', $this->createEntityStorage('paragraph_type'));
111
    $container->set('entity_type.manager.storage.field_config', $this->createEntityStorage('field_config'));
112
    $container->set('entity_type.manager.view_builder.paragraph', $this->createMockViewBuilder('paragraph'));
113
114
    // Set up the renderer mock.
115
    $prophecy = $this->prophesize(RendererInterface::CLASS);
116
    $prophecy->executeInRenderContext(Argument::cetera())->will(function ($args) {
117
      return $args[0]();
118
    });
119
    $prophecy->render(Argument::type('array'))->will(function ($args) {
120
      return $args[0]['tag'];
121
    });
122
    $container->set('renderer', $prophecy->reveal());
123
  }*/
124
125 5
  protected function generateTabs($storage, $tabs = 1) {
126
127 5
    $tab_paragraphs = [];
128 5
    for ($i = 0; $i < $tabs; $i++) {
129 4
      $tab_paragraph = $this->generateTab($storage, 'Tab ' . $i);
130 4
      $tab_paragraph->setNeedsSave(TRUE);
131 4
      $tab_paragraphs[] = [
132 4
        'entity' => $tab_paragraph,
133 4
        'target_id' => $tab_paragraph->id(),
134 4
        'target_revision_id' => $tab_paragraph->getRevisionId(),
135
      ];
136
    }
137
138 5
    $paragraph = $storage->create([
139 5
      'type' => 'tabs',
140
    ]);
141 5
    $paragraph->field_tabs->setValue($tab_paragraphs);
142 5
    return $paragraph;
143
  }
144
145 8
  protected function generateTab($storage, $title = 'Title') {
146 8
    $paragraph = $storage->create([
147 8
      'type' => 'tab',
148
    ]);
149 8
    $paragraph->field_title->value = $title;
150 8
    return $paragraph;
151
  }
152
153 2
  protected function generateEditorText($storage, $markup = 'test_markup', $format = 'default') {
154 2
    $paragraph = $storage->create([
155 2
      'type' => 'paragraphs_editor_text',
156
    ]);
157 2
    $paragraph->field_paragraphs_editor_text->setValue([
158 2
      'value' => $markup,
159 2
      'format' => $format,
160
    ]);
161 2
    return $paragraph;
162
  }
163
164
}
165