Completed
Push — 8.x-1.x ( 47b3da...df0680 )
by Janez
02:42
created

CropFunctionalTest::testCropTypeCrud()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 70
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 51
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 70
rs 9.1724

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\crop\Tests;
4
5
use Drupal\simpletest\WebTestBase;
6
7
/**
8
 * Functional tests for crop API.
9
 *
10
 * @group crop
11
 */
12
class CropFunctionalTest extends WebTestBase {
13
14
  /**
15
   * Modules to enable.
16
   *
17
   * @var array
18
   */
19
  public static $modules = ['crop'];
20
21
  /**
22
   * Admin user.
23
   *
24
   * @var \Drupal\user\UserInterface
25
   */
26
  protected $adminUser;
27
28
  /**
29
   * Test image style.
30
   *
31
   * @var \Drupal\image\ImageStyleInterface
32
   */
33
  protected $testStyle;
34
35
  /**
36
   * Test crop type.
37
   *
38
   * @var \Drupal\crop\CropInterface
39
   */
40
  protected $cropType;
41
42
  /**
43
   * {@inheritdoc}
44
   */
45
  protected function setUp() {
46
    parent::setUp();
47
48
    $this->adminUser = $this->drupalCreateUser(['administer crop types', 'administer image styles']);
49
50
    // Create test image style.
51
    $this->testStyle = $this->container->get('entity.manager')->getStorage('image_style')->create([
52
      'name' => 'test',
53
      'label' => 'Test image style',
54
      'effects' => [],
55
    ]);
56
    $this->testStyle->save();
57
  }
58
59
  /**
60
   * Tests crop type crud pages.
61
   */
62
  public function testCropTypeCrud() {
63
    // Anonymous users don't have access to crop type admin pages.
64
    $this->drupalGet('admin/structure/crop');
65
    $this->assertResponse(403);
66
    $this->drupalGet('admin/structure/crop/add');
67
    $this->assertResponse(403);
68
69
    // Can access pages if logged in and no crop types exist.
70
    $this->drupalLogin($this->adminUser);
71
    $this->drupalGet('admin/structure/crop');
72
    $this->assertResponse(200);
73
    $this->assertText(t('No crop types available.'));
74
    $this->assertLink(t('Add crop type'));
75
76
    // Can access add crop type form.
77
    $this->clickLink(t('Add crop type'));
78
    $this->assertResponse(200);
79
    $this->assertUrl('admin/structure/crop/add');
80
81
    // Create crop type.
82
    $edit = [
83
      'id' => strtolower($this->randomMachineName()),
84
      'label' => $this->randomMachineName(),
85
      'description' => $this->randomGenerator->sentences(10),
86
    ];
87
    $this->drupalPostForm('admin/structure/crop/add', $edit, t('Save crop type'));
88
    $this->assertRaw(t('The crop type %name has been added.', ['%name' => $edit['label']]));
89
    $this->assertUrl('admin/structure/crop');
90
    $label = $this->xpath("//td[contains(concat(' ',normalize-space(@class),' '),' menu-label ')]");
91
    $this->assert(strpos($label[0]->asXML(), $edit['label']) !== FALSE, 'Crop type label found on listing page.');
92
    $this->assertText($edit['description']);
93
94
    // Check edit form.
95
    $this->clickLink(t('Edit'));
96
    $this->assertText(t('Edit @name crop type', ['@name' => $edit['label']]));
97
    $this->assertRaw($edit['id']);
98
    $this->assertFieldById('edit-label', $edit['label']);
99
    $this->assertRaw($edit['description']);
100
101
    // See if crop type appears on image effect configuration form.
102
    $this->drupalGet('admin/config/media/image-styles/manage/' . $this->testStyle->id() . '/add/crop_crop');
103
    $option = $this->xpath("//select[@id='edit-data-crop-type']/option");
104
    $this->assert(strpos($option[0]->asXML(), $edit['label']) !== FALSE, 'Crop type label found on image effect page.');
105
    $this->drupalPostForm('admin/config/media/image-styles/manage/' . $this->testStyle->id() . '/add/crop_crop', ['data[crop_type]' => $edit['id']], t('Add effect'));
106
    $this->assertText(t('The image effect was successfully applied.'));
107
    $this->assertText(t('Manual crop uses @name crop type', ['@name' => $edit['label']]));
108
    $this->testStyle = $this->container->get('entity.manager')->getStorage('image_style')->loadUnchanged($this->testStyle->id());
109
    $this->assertEqual($this->testStyle->getEffects()->count(), 1, 'One image effect added to test image style.');
110
    $effect_configuration = $this->testStyle->getEffects()->getIterator()->current()->getConfiguration();
111
    $this->assertEqual($effect_configuration['data'], ['crop_type' => $edit['id']], 'Manual crop effect uses correct image style.');
112
113
    // Try to access edit form as anonymous user.
114
    $this->drupalLogout();
115
    $this->drupalGet('admin/structure/crop/manage/' . $edit['id']);
116
    $this->assertResponse(403);
117
    $this->drupalLogin($this->adminUser);
118
119
    // Try to create crop type with same machine name.
120
    $this->drupalPostForm('admin/structure/crop/add', $edit, t('Save crop type'));
121
    $this->assertText(t('The machine-readable name is already in use. It must be unique.'));
122
123
    // Delete crop type.
124
    $this->drupalGet('admin/structure/crop');
125
    $this->assertLink('Test image style');
126
    $this->clickLink(t('Delete'));
127
    $this->assertText(t('Are you sure you want to delete the crop type @name?', ['@name' => $edit['label']]));
128
    $this->drupalPostForm('admin/structure/crop/manage/' . $edit['id'] . '/delete', [], t('Delete'));
129
    $this->assertRaw(t('The crop type %name has been deleted.', ['%name' => $edit['label']]));
130
    $this->assertText(t('No crop types available.'));
131
  }
132
133
}
134