You are here

public function OgResolvedGroupCollectionTest::testVoteWeightDataType in Organic groups 8

Tests that only the right data type can be passed as vote weight.

@covers ::addGroup @covers ::setVoteWeight

@dataProvider mixedDataProvider

Parameters

mixed $weight: The value to pass as a vote weight.

File

tests/src/Unit/OgResolvedGroupCollectionTest.php, line 288

Class

OgResolvedGroupCollectionTest
Tests the collecting of resolved groups to pass as a route context.

Namespace

Drupal\Tests\og\Unit

Code

public function testVoteWeightDataType($weight) {
  $collection = new OgResolvedGroupCollection();
  $group = $this->groups[array_rand($this->groups)];

  // It should be possible to pass NULL as a custom vote weight when adding a
  // new vote, but not to set it as the default vote weight.
  if (is_null($weight)) {
    try {
      $collection
        ->addGroup($group, [], $weight);
    } catch (\InvalidArgumentException $e) {
      $this
        ->fail('It is possible to pass NULL as the group weight when adding a vote.');
    }
    try {
      $collection
        ->setVoteWeight($weight);
      $this
        ->fail('It is not possible to set NULL as the default group weight.');
    } catch (\InvalidArgumentException $e) {

      // Expected result.
    }

    // The default vote weight should still be 0.
    $this
      ->assertEquals(0, $collection
      ->getVoteWeight());
  }
  elseif (is_int($weight)) {
    $collection
      ->addGroup($group, [], $weight);
    $collection
      ->setVoteWeight($weight);

    // The default vote weight should be set to the value.
    $this
      ->assertEquals($weight, $collection
      ->getVoteWeight());
  }
  else {
    try {
      $collection
        ->addGroup($group, [], $weight);
      $this
        ->fail('Passing a non-integer value as the vote weight when adding a group throws an exception.');
    } catch (\InvalidArgumentException $e) {

      // Expected result.
    }
    try {
      $collection
        ->setVoteWeight($weight);
      $this
        ->fail('Setting a non-integer value as the vote weight throws an exception.');
    } catch (\InvalidArgumentException $e) {

      // Expected result.
    }

    // The default vote weight should still be 0.
    $this
      ->assertEquals(0, $collection
      ->getVoteWeight());
  }
}