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 |
||
| 20 | class EditTest extends WebTestCase |
||
| 21 | { |
||
| 22 | /** @var Container The DI container. */ |
||
| 23 | protected $container; |
||
| 24 | |||
| 25 | /** @var Project The project instance. */ |
||
| 26 | protected $project; |
||
| 27 | |||
| 28 | /** @var ProjectRepository The project repo instance. */ |
||
| 29 | protected $projectRepo; |
||
| 30 | |||
| 31 | /** @var Page The page instance. */ |
||
| 32 | protected $page; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Set up container, class instances and mocks. |
||
| 36 | */ |
||
| 37 | public function setUp() |
||
| 38 | { |
||
| 39 | $client = static::createClient(); |
||
| 40 | $this->container = $client->getContainer(); |
||
| 41 | |||
| 42 | $this->project = new Project('TestProject'); |
||
| 43 | $this->projectRepo = $this->getMock(ProjectRepository::class); |
||
| 44 | $this->projectRepo->method('getOne') |
||
| 45 | ->willReturn([ |
||
| 46 | 'url' => 'https://test.example.org', |
||
| 47 | 'dbName' => 'test_wiki', |
||
| 48 | 'lang' => 'en', |
||
| 49 | ]); |
||
| 50 | $this->project->setRepository($this->projectRepo); |
||
| 51 | $this->page = new Page($this->project, 'Test_page'); |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Test the basic functionality of Edit. |
||
| 56 | */ |
||
| 57 | public function testBasic() |
||
| 58 | { |
||
| 59 | $edit = new Edit($this->page, [ |
||
| 60 | 'id' => '1', |
||
| 61 | 'timestamp' => '20170101100000', |
||
| 62 | 'minor' => '0', |
||
| 63 | 'length' => '12', |
||
| 64 | 'length_change' => '2', |
||
| 65 | 'username' => 'Testuser', |
||
| 66 | 'comment' => 'Test', |
||
| 67 | ]); |
||
| 68 | $this->assertEquals($this->project, $edit->getProject()); |
||
| 69 | $this->assertInstanceOf(DateTime::class, $edit->getTimestamp()); |
||
| 70 | $this->assertEquals($this->page, $edit->getPage()); |
||
| 71 | $this->assertEquals('1483264800', $edit->getTimestamp()->getTimestamp()); |
||
| 72 | $this->assertEquals(1, $edit->getId()); |
||
| 73 | $this->assertFalse($edit->isMinor()); |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Wikified edit summary |
||
| 78 | */ |
||
| 79 | public function testWikifiedComment() |
||
| 80 | { |
||
| 81 | $edit = new Edit($this->page, [ |
||
| 82 | 'id' => '1', |
||
| 83 | 'timestamp' => '20170101100000', |
||
| 84 | 'minor' => '0', |
||
| 85 | 'length' => '12', |
||
| 86 | 'length_change' => '2', |
||
| 87 | 'username' => 'Testuser', |
||
| 88 | 'comment' => '<script>alert("XSS baby")</script> [[test page]]', |
||
| 89 | ]); |
||
| 90 | |||
| 91 | $this->assertEquals( |
||
| 92 | "<script>alert("XSS baby")</script> " . |
||
| 93 | "<a target='_blank' href='https://test.example.org/wiki/Test_page'>test page</a>", |
||
| 94 | $edit->getWikifiedSummary() |
||
| 95 | ); |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Make sure the right tool is detected |
||
| 100 | */ |
||
| 101 | public function testTool() |
||
| 102 | { |
||
| 103 | $edit = new Edit($this->page, [ |
||
| 104 | 'id' => '1', |
||
| 105 | 'timestamp' => '20170101100000', |
||
| 106 | 'minor' => '0', |
||
| 107 | 'length' => '12', |
||
| 108 | 'length_change' => '2', |
||
| 109 | 'username' => 'Testuser', |
||
| 110 | 'comment' => 'Level 2 warning re. [[Barack Obama]] ([[WP:HG|HG]]) (3.2.0)', |
||
| 111 | ]); |
||
| 112 | |||
| 113 | $this->assertArraySubset( |
||
| 114 | [ |
||
| 115 | 'name' => 'Huggle', |
||
| 116 | ], |
||
| 117 | $edit->getTool($this->container) |
||
| 118 | ); |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Was the edit a revert, based on the edit summary? |
||
| 123 | */ |
||
| 124 | public function testIsRevert() |
||
| 125 | { |
||
| 126 | $edit = new Edit($this->page, [ |
||
| 127 | 'id' => '1', |
||
| 128 | 'timestamp' => '20170101100000', |
||
| 129 | 'minor' => '0', |
||
| 130 | 'length' => '12', |
||
| 131 | 'length_change' => '2', |
||
| 132 | 'username' => 'Testuser', |
||
| 133 | 'comment' => 'You should have reverted this edit using [[WP:HG|Huggle]]', |
||
| 134 | ]); |
||
| 135 | |||
| 136 | $this->assertFalse($edit->isRevert($this->container)); |
||
| 137 | |||
| 138 | $edit2 = new Edit($this->page, [ |
||
| 139 | 'id' => '1', |
||
| 140 | 'timestamp' => '20170101100000', |
||
| 141 | 'minor' => '0', |
||
| 142 | 'length' => '12', |
||
| 143 | 'length_change' => '2', |
||
| 144 | 'username' => 'Testuser', |
||
| 145 | 'comment' => 'Reverted edits by Mogultalk (talk) ([[WP:HG|HG]]) (3.2.0)', |
||
| 146 | ]); |
||
| 147 | |||
| 148 | $this->assertTrue($edit2->isRevert($this->container)); |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Tests that given edit summary is properly asserted as a revert |
||
| 153 | */ |
||
| 154 | public function testIsAutomated() |
||
| 155 | { |
||
| 156 | $edit = new Edit($this->page, [ |
||
| 157 | 'id' => '1', |
||
| 158 | 'timestamp' => '20170101100000', |
||
| 159 | 'minor' => '0', |
||
| 160 | 'length' => '12', |
||
| 161 | 'length_change' => '2', |
||
| 162 | 'username' => 'Testuser', |
||
| 163 | 'comment' => 'You should have reverted this edit using [[WP:HG|Huggle]]', |
||
| 164 | ]); |
||
| 165 | |||
| 166 | $this->assertFalse($edit->isAutomated($this->container)); |
||
| 167 | |||
| 168 | $edit2 = new Edit($this->page, [ |
||
| 169 | 'id' => '1', |
||
| 170 | 'timestamp' => '20170101100000', |
||
| 171 | 'minor' => '0', |
||
| 172 | 'length' => '12', |
||
| 173 | 'length_change' => '2', |
||
| 174 | 'username' => 'Testuser', |
||
| 175 | 'comment' => 'Reverted edits by Mogultalk (talk) ([[WP:HG|HG]]) (3.2.0)', |
||
| 176 | ]); |
||
| 177 | |||
| 178 | $this->assertTrue($edit2->isAutomated($this->container)); |
||
| 179 | } |
||
| 180 | } |
||
| 181 |