You are here

public function CasAttributesSubscriberTest::roleMappingComparisonMethodsDataProvider in CAS Attributes 2.x

Same name and namespace in other branches
  1. 8 tests/src/Unit/Subscriber/CasAttributesSubscriberTest.php \Drupal\Tests\cas_attributes\Unit\Subscriber\CasAttributesSubscriberTest::roleMappingComparisonMethodsDataProvider()

Provides data for testing role mapping scenarioes.

Return value

array Parameters.

File

tests/src/Unit/Subscriber/CasAttributesSubscriberTest.php, line 355

Class

CasAttributesSubscriberTest
CasAttributesSubscriber unit tests.

Namespace

Drupal\Tests\cas_attributes\Unit\Subscriber

Code

public function roleMappingComparisonMethodsDataProvider() {
  $scenarios = [];

  // Role B should be added because the user has the exact attribute we're
  // looking for.
  $scenarios[] = [
    [
      'roles_before' => [
        'roleA',
      ],
      'roles_after' => [
        'roleA',
        'roleB',
      ],
      'attributes_user_has' => [
        'attrA' => [
          'bananas',
        ],
      ],
      'mappings' => [
        [
          'rid' => 'roleB',
          'method' => 'exact_single',
          'attribute' => 'attrA',
          'value' => 'bananas',
          'remove_without_match' => FALSE,
        ],
      ],
    ],
  ];

  // Same as above, but test that different cases for attribute names makes
  // no difference.
  $scenarios[] = [
    [
      'roles_before' => [
        'roleA',
      ],
      'roles_after' => [
        'roleA',
        'roleB',
      ],
      'attributes_user_has' => [
        'attra' => [
          'bananas',
        ],
      ],
      'mappings' => [
        [
          'rid' => 'roleB',
          'method' => 'exact_single',
          'attribute' => 'attrA',
          'value' => 'bananas',
          'remove_without_match' => FALSE,
        ],
      ],
    ],
  ];
  $scenarios[] = [
    [
      'roles_before' => [
        'roleA',
      ],
      'roles_after' => [
        'roleA',
        'roleB',
      ],
      'attributes_user_has' => [
        'attrA' => [
          'bananas',
        ],
      ],
      'mappings' => [
        [
          'rid' => 'roleB',
          'method' => 'exact_single',
          'attribute' => 'attra',
          'value' => 'bananas',
          'remove_without_match' => FALSE,
        ],
      ],
    ],
  ];

  // Role B will NOT be added because the value we're looking for is not an
  // exact match with that the user has.
  $scenarios[] = [
    [
      'roles_before' => [
        'roleA',
      ],
      'roles_after' => [
        'roleA',
      ],
      'attributes_user_has' => [
        'attrA' => [
          'bananas',
        ],
      ],
      'mappings' => [
        [
          'rid' => 'roleB',
          'method' => 'exact_single',
          'attribute' => 'attrA',
          'value' => 'banan',
          'remove_without_match' => FALSE,
        ],
      ],
    ],
  ];

  // Here we make sure that two role mappings that each add different roles
  // will work as expected.
  $scenarios[] = [
    [
      'roles_before' => [
        'roleA',
      ],
      'roles_after' => [
        'roleA',
        'roleB',
        'roleC',
      ],
      'attributes_user_has' => [
        'attrA' => [
          'bananas',
        ],
        'attrB' => [
          'cucumbers',
        ],
      ],
      'mappings' => [
        [
          'rid' => 'roleB',
          'method' => 'exact_single',
          'attribute' => 'attrA',
          'value' => 'bananas',
          'remove_without_match' => FALSE,
        ],
        [
          'rid' => 'roleC',
          'method' => 'exact_single',
          'attribute' => 'attrB',
          'value' => 'cucumbers',
          'remove_without_match' => FALSE,
        ],
      ],
    ],
  ];

  // Role B should not be added, because even though the attribute value
  // we're checking exists, it's part of a multi-value array, and the
  // method only works if it's a single value array.
  $scenarios[] = [
    [
      'roles_before' => [
        'roleA',
      ],
      'roles_after' => [
        'roleA',
      ],
      'attributes_user_has' => [
        'attrA' => [
          'bananas',
          'cucumbers',
        ],
      ],
      'mappings' => [
        [
          'rid' => 'roleB',
          'method' => 'exact_single',
          'attribute' => 'attrA',
          'value' => 'bananas',
          'remove_without_match' => FALSE,
        ],
      ],
    ],
  ];

  // However if we switch the method to variation that searches all items
  // in the attribute value array, it should work.
  $scenarios[] = [
    [
      'roles_before' => [
        'roleA',
      ],
      'roles_after' => [
        'roleA',
        'roleB',
      ],
      'attributes_user_has' => [
        'attrA' => [
          'bananas',
          'cucumbers',
        ],
      ],
      'mappings' => [
        [
          'rid' => 'roleB',
          'method' => 'exact_any',
          'attribute' => 'attrA',
          'value' => 'bananas',
          'remove_without_match' => FALSE,
        ],
      ],
    ],
  ];

  // Role A should be REMOVED from the user, because it's mapping fails.
  $scenarios[] = [
    [
      'roles_before' => [
        'roleA',
      ],
      'roles_after' => [
        'roleB',
      ],
      'attributes_user_has' => [
        'attrA' => [
          'bananas',
        ],
        'attrB' => [
          'cucumbers',
        ],
      ],
      'mappings' => [
        [
          'rid' => 'roleA',
          'method' => 'exact_single',
          'attribute' => 'attrA',
          'value' => 'cherries',
          'remove_without_match' => TRUE,
        ],
        [
          'rid' => 'roleB',
          'method' => 'exact_single',
          'attribute' => 'attrB',
          'value' => 'cucumbers',
          'remove_without_match' => FALSE,
        ],
      ],
    ],
  ];

  // Test that the 'contains' method works as expected. Student role should
  // be added because the value to check appears as a substring in the actual
  // value.
  $scenarios[] = [
    [
      'roles_before' => [],
      'roles_after' => [
        'student',
      ],
      'attributes_user_has' => [
        'groups' => [
          'Linux User',
          'First Year Student',
        ],
      ],
      'mappings' => [
        [
          'rid' => 'student',
          'method' => 'contains_any',
          'attribute' => 'groups',
          'value' => 'Student',
          'remove_without_match' => TRUE,
        ],
      ],
    ],
  ];

  // Test that the 'regex' method works as expected. Student role should be
  // added because the regex checking for a value passes.
  $scenarios[] = [
    [
      'roles_before' => [],
      'roles_after' => [
        'student',
      ],
      'attributes_user_has' => [
        'groups' => [
          'Linux User',
          'First Year Student',
        ],
      ],
      'mappings' => [
        [
          'rid' => 'student',
          'method' => 'regex_any',
          'attribute' => 'groups',
          'value' => '/.+student$/i',
          'remove_without_match' => TRUE,
        ],
      ],
    ],
  ];

  // Test that the 'negate' flag works.
  $scenarios[] = [
    [
      'roles_before' => [],
      'roles_after' => [
        'undergrad',
      ],
      'attributes_user_has' => [
        'groups' => [
          'Linux User',
          'First Year Student',
        ],
      ],
      'mappings' => [
        [
          'rid' => 'undergrad',
          'method' => 'contains_any',
          'attribute' => 'groups',
          'value' => 'Graduate Student',
          'negate' => TRUE,
          'remove_without_match' => TRUE,
        ],
      ],
    ],
    [
      'roles_before' => [],
      'roles_after' => [],
      'attributes_user_has' => [
        'groups' => [
          'Linux User',
          'Graduate Student',
        ],
      ],
      'mappings' => [
        [
          'rid' => 'undergrad',
          'method' => 'contains_any',
          'attribute' => 'groups',
          'value' => 'Graduate Student',
          'negate' => TRUE,
          'remove_without_match' => TRUE,
        ],
      ],
    ],
  ];
  return $scenarios;
}