From 2c4070e3d0b22b90ca503ff0a9f7667fc549ce7a Mon Sep 17 00:00:00 2001
From: Ide Braakman <ide@ezcompany.nl>
Date: Fri, 21 Dec 2018 14:07:29 +0100
Subject: [PATCH] Issue #3021997 by idebr: Convert automated tests from
 Simpletest to PHPUnit

---
 src/Tests/VoteCreationTest.php                   | 93 ------------------------
 tests/src/Functional/VoteCreationTest.php        | 72 ++++++++++++++++++
 {src/Tests => tests/src/Functional}/VoteTest.php | 36 ++++-----
 3 files changed, 90 insertions(+), 111 deletions(-)
 delete mode 100644 src/Tests/VoteCreationTest.php
 create mode 100644 tests/src/Functional/VoteCreationTest.php
 rename {src/Tests => tests/src/Functional}/VoteTest.php (74%)

diff --git a/src/Tests/VoteCreationTest.php b/src/Tests/VoteCreationTest.php
deleted file mode 100644
index eed8133..0000000
--- a/src/Tests/VoteCreationTest.php
+++ /dev/null
@@ -1,93 +0,0 @@
-<?php
-
-namespace Drupal\votingapi\Tests;
-
-use Drupal\node\Entity\Node;
-use Drupal\simpletest\WebTestBase;
-use Drupal\votingapi\Entity\Vote;
-
-/**
- * Tests the Voting API basics.
- *
- * @group VotingAPI
- */
-class VoteCreationTest extends WebTestBase {
-
-  /**
-   * Modules to enable.
-   *
-   * @var array
-   */
-  public static $modules = ['node', 'votingapi', 'votingapi_test'];
-
-  /**
-   * A simple user with basic node and vote permissions.
-   *
-   * @var \Drupal\user\Entity\User
-   */
-  protected $logged_user;
-
-  /**
-   * A simple user vote permission.
-   *
-   * @var \Drupal\user\Entity\User
-   */
-  protected $anonymous_user;
-
-  /**
-   * @var \Drupal\node\Entity\Node
-   */
-  private $node;
-
-  /**
-   * @var \Drupal\votingapi\Entity\Vote
-   */
-  private $vote;
-
-  /**
-   *
-   */
-  protected function setUp() {
-    parent::setUp();
-
-    // Create Basic page and Article node types.
-    if ($this->profile != 'standard') {
-      $node_type = $this->drupalCreateContentType([
-        'type' => 'page',
-        'name' => 'Basic page',
-        'display_submitted' => FALSE,
-      ]);
-      node_add_body_field($node_type);
-    }
-
-    $this->logged_user = $this->drupalCreateUser();
-
-    $this->drupalLogin($this->logged_user);
-
-    $title = $this->randomMachineName(8);
-
-    $this->node = Node::create(['type' => 'page']);
-    $this->node->setOwner($this->loggedInUser);
-    $this->node->setTitle($title);
-    $this->node->set('body', ['value' => $this->randomMachineName(16)]);
-    $this->node->save();
-
-    $this->node = $this->drupalGetNodeByTitle($title);
-    $this->assertTrue($this->node, 'Basic page created for Voting API tests.');
-  }
-
-  /**
-   * Test voting with non-existent Vote Type Id.
-   */
-  public function testVoteCreationWithInvalidVoteType() {
-
-    $this->vote = Vote::create(['type' => 'vote']);
-    // $this->vote = Vote::create(['type' => 'fake_vote_type']);.
-    $this->vote->setVotedEntityId($this->node->id());
-    $this->vote->setVotedEntityType($this->node->getEntityTypeId());
-    $this->vote->setValue(50);
-    $this->vote->save();
-    $this->assertTrue($this->vote, 'A "fake_vote_type" vote was successfully cast on a node.');
-  }
-
-}
diff --git a/tests/src/Functional/VoteCreationTest.php b/tests/src/Functional/VoteCreationTest.php
new file mode 100644
index 0000000..a61d3b5
--- /dev/null
+++ b/tests/src/Functional/VoteCreationTest.php
@@ -0,0 +1,72 @@
+<?php
+
+namespace Drupal\votingapi\Tests;
+
+use Drupal\node\Entity\Node;
+use Drupal\Tests\BrowserTestBase;
+use Drupal\votingapi\Entity\Vote;
+
+/**
+ * Tests the Voting API basics.
+ *
+ * @group VotingAPI
+ */
+class VoteCreationTest extends BrowserTestBase {
+
+  /**
+   * {@inheritdoc}
+   */
+  public static $modules = ['node', 'votingapi', 'votingapi_test'];
+
+  /**
+   * A test node.
+   *
+   * @var \Drupal\node\Entity\Node
+   */
+  private $node;
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function setUp() {
+    parent::setUp();
+
+    // Create Basic page and Article node types.
+    if ($this->profile != 'standard') {
+      $node_type = $this->drupalCreateContentType([
+        'type' => 'page',
+        'name' => 'Basic page',
+        'display_submitted' => FALSE,
+      ]);
+      node_add_body_field($node_type);
+    }
+
+    $this->drupalLogin($this->drupalCreateUser());
+
+    $title = $this->randomMachineName(8);
+
+    $node = Node::create(['type' => 'page']);
+    $node->setOwner($this->loggedInUser);
+    $node->setTitle($title);
+    $node->set('body', ['value' => $this->randomMachineName(16)]);
+    $node->save();
+
+    $this->node = $this->drupalGetNodeByTitle($title);
+    $this->assertTrue($this->node, 'Basic page created for Voting API tests.');
+  }
+
+  /**
+   * Test voting with non-existent Vote Type Id.
+   */
+  public function testVoteCreationWithInvalidVoteType() {
+
+    $vote = Vote::create(['type' => 'vote']);
+    // $vote = Vote::create(['type' => 'fake_vote_type']);.
+    $vote->setVotedEntityId($this->node->id());
+    $vote->setVotedEntityType($this->node->getEntityTypeId());
+    $vote->setValue(50);
+    $vote->save();
+    $this->assertTrue($vote, 'A "fake_vote_type" vote was successfully cast on a node.');
+  }
+
+}
diff --git a/src/Tests/VoteTest.php b/tests/src/Functional/VoteTest.php
similarity index 74%
rename from src/Tests/VoteTest.php
rename to tests/src/Functional/VoteTest.php
index c732869..7b7489b 100644
--- a/src/Tests/VoteTest.php
+++ b/tests/src/Functional/VoteTest.php
@@ -2,14 +2,14 @@
 
 namespace Drupal\votingapi\Tests;
 
-use Drupal\simpletest\WebTestBase;
+use Drupal\Tests\BrowserTestBase;
 
 /**
  * Tests the Voting API basics.
  *
  * @group VotingAPI
  */
-class VoteTest extends WebTestBase {
+class VoteTest extends BrowserTestBase {
 
   /**
    * {@inheritdoc}
@@ -29,7 +29,7 @@ class VoteTest extends WebTestBase {
     $query = $vote_query->condition('entity_type', 'node')
       ->condition('entity_id', $node->id());
     $votes = $query->execute();
-    $this->assertEqual(count($votes), 0, 'Vote count for a node is initially zero.');
+    $this->assertEquals(count($votes), 0, 'Vote count for a node is initially zero.');
 
     // Add a vote to a node.
     /** @var \Drupal\votingapi\VoteInterface $vote */
@@ -42,12 +42,12 @@ class VoteTest extends WebTestBase {
     ]);
     $vote->save();
     $votes = $query->execute();
-    $this->assertEqual(count($votes), 1, 'After a vote is cast on a node, it can be retrieved.');
+    $this->assertEquals(count($votes), 1, 'After a vote is cast on a node, it can be retrieved.');
     $vote = $vote_storage->load(reset($votes));
     $this->assertNotNull($vote, 'Node vote was loaded.');
-    $this->assertEqual($vote->getOwnerId(), $user->id(), 'Node vote has correct user.');
-    $this->assertEqual($vote->getValue(), -1, 'Node vote has correct value.');
-    $this->assertNotEqual($vote->getSource(), '', 'A vote with no explicit source received the default value.');
+    $this->assertEquals($vote->getOwnerId(), $user->id(), 'Node vote has correct user.');
+    $this->assertEquals($vote->getValue(), -1, 'Node vote has correct value.');
+    $this->assertNotEquals($vote->getSource(), '', 'A vote with no explicit source received the default value.');
 
     // Add a vote to a user.
     $vote = $vote_storage->create([
@@ -61,16 +61,16 @@ class VoteTest extends WebTestBase {
     $query = $vote_query->condition('entity_type', 'user')
       ->condition('entity_id', $user->id());
     $votes = $query->execute();
-    $this->assertEqual(count($votes), 1, 'After a vote is cast on a user, it can be retrieved.');
+    $this->assertEquals(count($votes), 1, 'After a vote is cast on a user, it can be retrieved.');
     $vote = $vote_storage->load(reset($votes));
     $this->assertNotNull($vote, 'User vote was loaded.');
-    $this->assertEqual($vote->getOwnerId(), 0, 'A vote with no explicit user received the default value.');
-    $this->assertEqual($vote->getValue(), 0, 'A vote with no explicit value received the default value.');
+    $this->assertEquals($vote->getOwnerId(), 0, 'A vote with no explicit user received the default value.');
+    $this->assertEquals($vote->getValue(), 0, 'A vote with no explicit value received the default value.');
 
     // Deleting entity deletes votes.
     entity_delete_multiple('user', [$user->id()]);
     $votes = $query->execute();
-    $this->assertEqual(count($votes), 0, 'When an entity is deleted, the votes are also deleted.');
+    $this->assertEquals(count($votes), 0, 'When an entity is deleted, the votes are also deleted.');
   }
 
   /**
@@ -99,9 +99,9 @@ class VoteTest extends WebTestBase {
     // Standard results are available and correct.
     $this->assertFalse(empty($results['vote']), 'Results for test vote type are available.');
     $this->assertTrue(isset($results['vote']['vote_sum']), 'Sum was calculated.');
-    $this->assertEqual($results['vote']['vote_sum'], 90, 'Sum is correct.');
+    $this->assertEquals($results['vote']['vote_sum'], 90, 'Sum is correct.');
     $this->assertTrue(isset($results['vote']['vote_average']), 'Average was calculated.');
-    $this->assertEqual($results['vote']['vote_average'], 30, 'Average is correct.');
+    $this->assertEquals($results['vote']['vote_average'], 30, 'Average is correct.');
 
     // When you remove a result type via the hook, it is not longer available.
     $this->assertTrue(empty($results['test']['vote_count']), 'Result removed via alter hook was not calculated.');
@@ -110,7 +110,7 @@ class VoteTest extends WebTestBase {
     // @todo This isn't working, as $results are pulled directly from the
     // database via the getResults() method.
     // $this->assertTrue(isset($results['vote']['zebra']), 'New result was calculated.');
-    // $this->assertEqual($results['vote']['zebra'], 10101, 'New result is correct.');.
+    // $this->assertEquals($results['vote']['zebra'], 10101, 'New result is correct.');.
     // Deleting entity removes results.
     entity_delete_multiple('node', [$node->id()]);
     $results = $manager->getResults('node', $node->id());
@@ -144,15 +144,15 @@ class VoteTest extends WebTestBase {
     // Retrieve the votes. For now, just count them.
     $votes_from_source_1 = $vote_storage->getUserVotes(0, 'vote', 'node', 1, 'source_1');
     $votes_from_source_2 = $vote_storage->getUserVotes(0, 'vote', 'node', 1, 'source_2');
-    $this->assertEqual(count($votes_from_source_1), 1, 'There is 1 vote from the first source.');
-    $this->assertEqual(count($votes_from_source_2), 2, 'There are 2 votes from the second source.');
+    $this->assertEquals(count($votes_from_source_1), 1, 'There is 1 vote from the first source.');
+    $this->assertEquals(count($votes_from_source_2), 2, 'There are 2 votes from the second source.');
 
     // Delete the votes from source_2 and repeat the test.
     $vote_storage->deleteUserVotes(0, 'vote', 'node', 1, 'source_2');
     $votes_from_source_1 = $vote_storage->getUserVotes(0, 'vote', 'node', 1, 'source_1');
     $votes_from_source_2 = $vote_storage->getUserVotes(0, 'vote', 'node', 1, 'source_2');
-    $this->assertEqual(count($votes_from_source_1), 1, 'There is still 1 vote from the first source.');
-    $this->assertEqual(count($votes_from_source_2), 0, 'There are now 0 votes from the second source.');
+    $this->assertEquals(count($votes_from_source_1), 1, 'There is still 1 vote from the first source.');
+    $this->assertEquals(count($votes_from_source_2), 0, 'There are now 0 votes from the second source.');
   }
 
 }
-- 
2.14.3 (Apple Git-98)

