Completed
Push — create-endpoint-fetcher ( 23bf41 )
by Sam
07:40
created

HtmlEditorConfigTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 129
Duplicated Lines 10.08 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 9
c 1
b 1
f 0
lcom 2
cbo 5
dl 13
loc 129
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A testEnablePluginsByString() 0 5 1
A testEnablePluginsByArray() 0 6 1
A testEnablePluginsByMultipleStringParameters() 6 6 1
A testEnablePluginsByArrayWithPaths() 0 63 1
A testDisablePluginsByString() 0 6 1
A testDisablePluginsByArray() 0 7 1
A testDisablePluginsByMultipleStringParameters() 7 7 1
A testDisablePluginsByArrayWithPaths() 0 8 1
A testRequireJSIncludesAllConfigs() 0 10 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * @package framework
4
 * @subpackage tests
5
 */
6
class HtmlEditorConfigTest extends SapphireTest {
7
8
	public function testEnablePluginsByString() {
9
		$c = new TinyMCEConfig();
10
		$c->enablePlugins('plugin1');
11
		$this->assertContains('plugin1', array_keys($c->getPlugins()));
12
	}
13
14
	public function testEnablePluginsByArray() {
15
		$c = new TinyMCEConfig();
16
		$c->enablePlugins(array('plugin1', 'plugin2'));
17
		$this->assertContains('plugin1', array_keys($c->getPlugins()));
18
		$this->assertContains('plugin2', array_keys($c->getPlugins()));
19
	}
20
21 View Code Duplication
	public function testEnablePluginsByMultipleStringParameters() {
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...
22
		$c = new TinyMCEConfig();
23
		$c->enablePlugins('plugin1', 'plugin2');
24
		$this->assertContains('plugin1', array_keys($c->getPlugins()));
25
		$this->assertContains('plugin2', array_keys($c->getPlugins()));
26
	}
27
28
	public function testEnablePluginsByArrayWithPaths() {
29
		Config::inst()->update('Director', 'alternate_base_url', 'http://mysite.com/subdir');
30
		$c = new TinyMCEConfig();
31
		$c->setTheme('modern');
32
		$c->setOption('language', 'es');
33
		$c->disablePlugins('table', 'emoticons', 'paste', 'code', 'link', 'importcss');
34
		$c->enablePlugins(array(
35
			'plugin1' => 'mypath/plugin1.js',
36
			'plugin2' => '/anotherbase/mypath/plugin2.js',
37
			'plugin3' => 'https://www.google.com/plugin.js',
38
			'plugin4' => null,
39
			'plugin5' => null,
40
		));
41
		$attributes = $c->getAttributes();
42
		$config = Convert::json2array($attributes['data-config']);
43
		$plugins = $config['external_plugins'];
44
		$this->assertNotEmpty($plugins);
45
46
		// Plugin specified via relative url
47
		$this->assertContains('plugin1', array_keys($plugins));
48
		$this->assertEquals(
49
			'http://mysite.com/subdir/mypath/plugin1.js',
50
			$plugins['plugin1']
51
		);
52
53
		// Plugin specified via root-relative url
54
		$this->assertContains('plugin2', array_keys($plugins));
55
		$this->assertEquals(
56
			'http://mysite.com/anotherbase/mypath/plugin2.js',
57
			$plugins['plugin2']
58
		);
59
60
		// Plugin specified with absolute url
61
		$this->assertContains('plugin3', array_keys($plugins));
62
		$this->assertEquals(
63
			'https://www.google.com/plugin.js',
64
			$plugins['plugin3']
65
		);
66
67
		// Plugin specified with standard location
68
		$this->assertContains('plugin4', array_keys($plugins));
69
		$this->assertEquals(
70
			'http://mysite.com/subdir/framework/thirdparty/tinymce/plugins/plugin4/plugin.min.js',
71
			$plugins['plugin4']
72
		);
73
74
		// Check that internal plugins are extractable separately
75
		$this->assertEquals(['plugin4', 'plugin5'], $c->getInternalPlugins());
76
77
		// Test plugins included via gzip compresser
78
		Config::inst()->update('HtmlEditorField', 'use_gzip', true);
79
		$this->assertEquals(
80
			'framework/thirdparty/tinymce/tiny_mce_gzip.php?js=1&plugins=plugin4,plugin5&themes=modern&languages=es&diskcache=true&src=true',
81
			$c->getScriptURL()
82
		);
83
84
		// If gzip is disabled only the core plugin is loaded
85
		Config::inst()->remove('HtmlEditorField', 'use_gzip');
86
		$this->assertEquals(
87
			'framework/thirdparty/tinymce/tinymce.min.js',
88
			$c->getScriptURL()
89
		);
90
	}
91
92
	public function testDisablePluginsByString() {
93
		$c = new TinyMCEConfig();
94
		$c->enablePlugins('plugin1');
95
		$c->disablePlugins('plugin1');
96
		$this->assertNotContains('plugin1', array_keys($c->getPlugins()));
97
	}
98
99
	public function testDisablePluginsByArray() {
100
		$c = new TinyMCEConfig();
101
		$c->enablePlugins(array('plugin1', 'plugin2'));
102
		$c->disablePlugins(array('plugin1', 'plugin2'));
103
		$this->assertNotContains('plugin1', array_keys($c->getPlugins()));
104
		$this->assertNotContains('plugin2', array_keys($c->getPlugins()));
105
	}
106
107 View Code Duplication
	public function testDisablePluginsByMultipleStringParameters() {
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...
108
		$c = new TinyMCEConfig();
109
		$c->enablePlugins('plugin1', 'plugin2');
110
		$c->disablePlugins('plugin1', 'plugin2');
111
		$this->assertNotContains('plugin1', array_keys($c->getPlugins()));
112
		$this->assertNotContains('plugin2', array_keys($c->getPlugins()));
113
	}
114
115
	public function testDisablePluginsByArrayWithPaths() {
116
		$c = new TinyMCEConfig();
117
		$c->enablePlugins(array('plugin1' => '/mypath/plugin1', 'plugin2' => '/mypath/plugin2'));
118
		$c->disablePlugins(array('plugin1', 'plugin2'));
119
		$plugins = $c->getPlugins();
120
		$this->assertNotContains('plugin1', array_keys($plugins));
121
		$this->assertNotContains('plugin2', array_keys($plugins));
122
	}
123
124
	public function testRequireJSIncludesAllConfigs() {
125
		$a = HtmlEditorConfig::get('configA');
126
		$c = HtmlEditorConfig::get('configB');
127
128
		$aAttributes = $a->getAttributes();
129
		$cAttributes = $c->getAttributes();
130
131
		$this->assertNotEmpty($aAttributes['data-config']);
132
		$this->assertNotEmpty($cAttributes['data-config']);
133
	}
134
}
135