diff --git a/tests/src/Kernel/FlagKernelTestBase.php b/tests/src/Kernel/FlagKernelTestBase.php index 4670dd8..bdc36e9 100644 --- a/tests/src/Kernel/FlagKernelTestBase.php +++ b/tests/src/Kernel/FlagKernelTestBase.php @@ -1,7 +1,6 @@ installSchema('node', ['node_access']); $this->flagService = \Drupal::service('flag'); - - // Create a flag. - $this->flag = Flag::create([ - 'id' => strtolower($this->randomMachineName()), - 'label' => $this->randomString(), - 'entity_type' => 'node', - 'bundles' => ['article'], - 'flag_type' => 'entity:node', - 'link_type' => 'reload', - 'flagTypeConfig' => [], - 'linkTypeConfig' => [], - ]); - $this->flag->save(); } } diff --git a/tests/src/Kernel/FlagServiceTest.php b/tests/src/Kernel/FlagServiceTest.php index e528f77..a907f41 100644 --- a/tests/src/Kernel/FlagServiceTest.php +++ b/tests/src/Kernel/FlagServiceTest.php @@ -1,6 +1,7 @@ strtolower($this->randomMachineName()), + 'label' => $this->randomString(), + 'entity_type' => 'node', + 'bundles' => ['article'], + 'flag_type' => 'entity:node', + 'link_type' => 'reload', + 'flagTypeConfig' => [], + 'linkTypeConfig' => [], + ]); + $flag->save(); + $result = $this->flagService->getFlags('node', 'article'); $this->assertIdentical(count($result), 1, 'Found flag type'); } @@ -29,11 +43,24 @@ class FlagServiceTest extends FlagKernelTestBase { // The service methods don't check access, so our user can be anybody. $account = $this->createUser(); + // Create a flag. + $flag = Flag::create([ + 'id' => strtolower($this->randomMachineName()), + 'label' => $this->randomString(), + 'entity_type' => 'node', + 'bundles' => ['article'], + 'flag_type' => 'entity:node', + 'link_type' => 'reload', + 'flagTypeConfig' => [], + 'linkTypeConfig' => [], + ]); + $flag->save(); + // Test flagging. // Try flagging an entity that's not a node: a user account. try { - $this->flagService->flag($this->flag, $account, $account); + $this->flagService->flag($flag, $account, $account); $this->fail("The exception was not thrown."); } catch (\LogicException $e) { @@ -48,7 +75,7 @@ class FlagServiceTest extends FlagKernelTestBase { $wrong_node->save(); try { - $this->flagService->flag($this->flag, $wrong_node, $account); + $this->flagService->flag($flag, $wrong_node, $account); $this->fail("The exception was not thrown."); } catch (\LogicException $e) { @@ -62,10 +89,10 @@ class FlagServiceTest extends FlagKernelTestBase { ]); $flaggable_node->save(); - $this->flagService->flag($this->flag, $flaggable_node, $account); + $this->flagService->flag($flag, $flaggable_node, $account); try { - $this->flagService->flag($this->flag, $flaggable_node, $account); + $this->flagService->flag($flag, $flaggable_node, $account); $this->fail("The exception was not thrown."); } catch (\LogicException $e) { @@ -75,7 +102,7 @@ class FlagServiceTest extends FlagKernelTestBase { // Try unflagging an entity that's not a node: a user account. try { - $this->flagService->unflag($this->flag, $account, $account); + $this->flagService->unflag($flag, $account, $account); $this->fail("The exception was not thrown."); } catch (\LogicException $e) { @@ -84,7 +111,7 @@ class FlagServiceTest extends FlagKernelTestBase { // Try unflagging a node of the wrong bundle. try { - $this->flagService->unflag($this->flag, $wrong_node, $account); + $this->flagService->unflag($flag, $wrong_node, $account); $this->fail("The exception was not thrown."); } catch (\LogicException $e) { @@ -98,7 +125,7 @@ class FlagServiceTest extends FlagKernelTestBase { ]); $unflagged_node->save(); try { - $this->flagService->unflag($this->flag, $unflagged_node, $account); + $this->flagService->unflag($flag, $unflagged_node, $account); $this->fail("The exception was not thrown."); } catch (\LogicException $e) { @@ -113,6 +140,19 @@ class FlagServiceTest extends FlagKernelTestBase { // The service methods don't check access, so our user can be anybody. $accounts = array($this->createUser(), $this->createUser()); + // Create a flag. + $flag = Flag::create([ + 'id' => strtolower($this->randomMachineName()), + 'label' => $this->randomString(), + 'entity_type' => 'node', + 'bundles' => ['article'], + 'flag_type' => 'entity:node', + 'link_type' => 'reload', + 'flagTypeConfig' => [], + 'linkTypeConfig' => [], + ]); + $flag->save(); + // Flag the node. $flaggable_node = Node::create([ 'type' => 'article', @@ -120,10 +160,10 @@ class FlagServiceTest extends FlagKernelTestBase { ]); $flaggable_node->save(); foreach ($accounts as $account) { - $this->flagService->flag($this->flag, $flaggable_node, $account); + $this->flagService->flag($flag, $flaggable_node, $account); } - $flagging_users = $this->flagService->getFlaggingUsers($flaggable_node, $this->flag); + $flagging_users = $this->flagService->getFlaggingUsers($flaggable_node, $flag); $this->assertTrue(is_array($flagging_users), "The method getFlaggingUsers() returns an array."); foreach ($accounts as $account) {