You are here

public function EntityAccessControlHandlerTest::accessProvider in Entity API 8

Data provider for testAccess().

Return value

array A list of testAccess method arguments.

File

tests/src/Unit/EntityAccessControlHandlerTest.php, line 81

Class

EntityAccessControlHandlerTest
@coversDefaultClass \Drupal\entity\EntityAccessControlHandler @group entity

Namespace

Drupal\Tests\entity\Unit

Code

public function accessProvider() {
  $entity_type = $this
    ->prophesize(ContentEntityTypeInterface::class);
  $entity_type
    ->id()
    ->willReturn('green_entity');
  $entity_type
    ->getAdminPermission()
    ->willReturn('administer green_entity');
  $entity_type
    ->hasHandlerClass('permission_provider')
    ->willReturn(TRUE);
  $entity_type
    ->getHandlerClass('permission_provider')
    ->willReturn(EntityPermissionProvider::class);
  $entity = $this
    ->buildMockEntity($entity_type
    ->reveal(), 6);
  $data = [];

  // Admin permission.
  $admin_user = $this
    ->buildMockUser(5, 'administer green_entity');
  $data['admin user, view'] = [
    $entity
      ->reveal(),
    'view',
    $admin_user
      ->reveal(),
    TRUE,
    [
      'user.permissions',
    ],
  ];
  $data['admin user, update'] = [
    $entity
      ->reveal(),
    'update',
    $admin_user
      ->reveal(),
    TRUE,
    [
      'user.permissions',
    ],
  ];
  $data['admin user, duplicate'] = [
    $entity
      ->reveal(),
    'duplicate',
    $admin_user
      ->reveal(),
    TRUE,
    [
      'user.permissions',
    ],
  ];
  $data['admin user, delete'] = [
    $entity
      ->reveal(),
    'delete',
    $admin_user
      ->reveal(),
    TRUE,
    [
      'user.permissions',
    ],
  ];

  // View, update, duplicate, delete permissions, entity without an owner.
  $second_entity = $this
    ->buildMockEntity($entity_type
    ->reveal());
  foreach ([
    'view',
    'update',
    'duplicate',
    'delete',
  ] as $operation) {
    $first_user = $this
      ->buildMockUser(6, $operation . ' green_entity');
    $second_user = $this
      ->buildMockUser(7, 'access content');
    $data["first user, {$operation}, entity without owner"] = [
      $second_entity
        ->reveal(),
      $operation,
      $first_user
        ->reveal(),
      TRUE,
      [
        'user.permissions',
      ],
    ];
    $data["second user, {$operation}, entity without owner"] = [
      $second_entity
        ->reveal(),
      $operation,
      $second_user
        ->reveal(),
      FALSE,
      [
        'user.permissions',
      ],
    ];
  }

  // Update, duplicate, and delete permissions.
  foreach ([
    'update',
    'duplicate',
    'delete',
  ] as $operation) {

    // Owner, non-owner, user with "any" permission.
    $first_user = $this
      ->buildMockUser(6, $operation . ' own green_entity');
    $second_user = $this
      ->buildMockUser(7, $operation . ' own green_entity');
    $third_user = $this
      ->buildMockUser(8, $operation . ' any green_entity');
    $data["first user, {$operation}, entity with owner"] = [
      $entity
        ->reveal(),
      $operation,
      $first_user
        ->reveal(),
      TRUE,
      [
        'user',
        'user.permissions',
      ],
    ];
    $data["second user, {$operation}, entity with owner"] = [
      $entity
        ->reveal(),
      $operation,
      $second_user
        ->reveal(),
      FALSE,
      [
        'user',
        'user.permissions',
      ],
    ];
    $data["third user, {$operation}, entity with owner"] = [
      $entity
        ->reveal(),
      $operation,
      $third_user
        ->reveal(),
      TRUE,
      [
        'user.permissions',
      ],
    ];
  }

  // View permissions.
  $first_user = $this
    ->buildMockUser(9, 'view green_entity');
  $second_user = $this
    ->buildMockUser(10, 'view first green_entity');
  $third_user = $this
    ->buildMockUser(14, 'view own unpublished green_entity');
  $fourth_user = $this
    ->buildMockUser(14, 'access content');
  $first_entity = $this
    ->buildMockEntity($entity_type
    ->reveal(), 1, 'first');
  $second_entity = $this
    ->buildMockEntity($entity_type
    ->reveal(), 1, 'second');
  $third_entity = $this
    ->buildMockEntity($entity_type
    ->reveal(), 14, 'first', FALSE);

  // The first user can view the two published entities.
  $data['first user, view, first entity'] = [
    $first_entity
      ->reveal(),
    'view',
    $first_user
      ->reveal(),
    TRUE,
    [
      'user.permissions',
    ],
  ];
  $data['first user, view, second entity'] = [
    $second_entity
      ->reveal(),
    'view',
    $first_user
      ->reveal(),
    TRUE,
    [
      'user.permissions',
    ],
  ];
  $data['first user, view, third entity'] = [
    $third_entity
      ->reveal(),
    'view',
    $first_user
      ->reveal(),
    FALSE,
    [
      'user',
    ],
  ];

  // The second user can only view published entities of bundle "first".
  $data['second user, view, first entity'] = [
    $first_entity
      ->reveal(),
    'view',
    $second_user
      ->reveal(),
    TRUE,
    [
      'user.permissions',
    ],
  ];
  $data['second user, view, second entity'] = [
    $second_entity
      ->reveal(),
    'view',
    $second_user
      ->reveal(),
    FALSE,
    [
      'user.permissions',
    ],
  ];
  $data['second user, view, third entity'] = [
    $third_entity
      ->reveal(),
    'view',
    $second_user
      ->reveal(),
    FALSE,
    [
      'user',
    ],
  ];

  // The third user can view their own unpublished entity.
  $data['third user, view, first entity'] = [
    $first_entity
      ->reveal(),
    'view',
    $third_user
      ->reveal(),
    FALSE,
    [
      'user.permissions',
    ],
  ];
  $data['third user, view, second entity'] = [
    $second_entity
      ->reveal(),
    'view',
    $third_user
      ->reveal(),
    FALSE,
    [
      'user.permissions',
    ],
  ];
  $data['third user, view, third entity'] = [
    $third_entity
      ->reveal(),
    'view',
    $third_user
      ->reveal(),
    TRUE,
    [
      'user',
      'user.permissions',
    ],
  ];

  // The fourth user can't view anything.
  $data['fourth user, view, first entity'] = [
    $first_entity
      ->reveal(),
    'view',
    $fourth_user
      ->reveal(),
    FALSE,
    [
      'user.permissions',
    ],
  ];
  $data['fourth user, view, second entity'] = [
    $second_entity
      ->reveal(),
    'view',
    $fourth_user
      ->reveal(),
    FALSE,
    [
      'user.permissions',
    ],
  ];
  $data['fourth user, view, third entity'] = [
    $third_entity
      ->reveal(),
    'view',
    $fourth_user
      ->reveal(),
    FALSE,
    [
      'user',
      'user.permissions',
    ],
  ];
  return $data;
}