/**
  * Tests user selection by roles.
  */
 function testUserSelectionByRole()
 {
     $field_definition = FieldConfig::loadByName('user', 'user', 'user_reference');
     $handler_settings = $field_definition->getSetting('handler_settings');
     $handler_settings['filter']['role'] = array($this->role1->id() => $this->role1->id(), $this->role2->id() => 0);
     $handler_settings['filter']['type'] = 'role';
     $field_definition->setSetting('handler_settings', $handler_settings);
     $field_definition->save();
     $user1 = $this->createUser(array('name' => 'aabb'));
     $user1->addRole($this->role1->id());
     $user1->save();
     $user2 = $this->createUser(array('name' => 'aabbb'));
     $user2->addRole($this->role1->id());
     $user2->save();
     $user3 = $this->createUser(array('name' => 'aabbbb'));
     $user3->addRole($this->role2->id());
     $user3->save();
     /** @var \Drupal\Core\Entity\EntityAutocompleteMatcher $autocomplete */
     $autocomplete = \Drupal::service('entity.autocomplete_matcher');
     $matches = $autocomplete->getMatches('user', 'default', $field_definition->getSetting('handler_settings'), 'aabb');
     $this->assertEqual(count($matches), 2);
     $users = array();
     foreach ($matches as $match) {
         $users[] = $match['label'];
     }
     $this->assertTrue(in_array($user1->label(), $users));
     $this->assertTrue(in_array($user2->label(), $users));
     $this->assertFalse(in_array($user3->label(), $users));
     $matches = $autocomplete->getMatches('user', 'default', $field_definition->getSetting('handler_settings'), 'aabbbb');
     $this->assertEqual(count($matches), 0, '');
 }
Пример #2
0
 /**
  * @covers ::getOperations
  */
 public function testGetOperations()
 {
     $operation_name = $this->randomMachineName();
     $operations = array($operation_name => array('title' => $this->randomMachineName()));
     $this->moduleHandler->expects($this->once())->method('invokeAll')->with('entity_operation', array($this->role))->will($this->returnValue($operations));
     $this->moduleHandler->expects($this->once())->method('alter')->with('entity_operation');
     $this->container->set('module_handler', $this->moduleHandler);
     $this->role->expects($this->any())->method('access')->will($this->returnValue(AccessResult::allowed()));
     $this->role->expects($this->any())->method('hasLinkTemplate')->will($this->returnValue(TRUE));
     $url = $this->getMockBuilder('\\Drupal\\Core\\Url')->disableOriginalConstructor()->getMock();
     $url->expects($this->any())->method('toArray')->will($this->returnValue(array()));
     $this->role->expects($this->any())->method('urlInfo')->will($this->returnValue($url));
     $list = new EntityListBuilder($this->entityType, $this->roleStorage, $this->moduleHandler);
     $list->setStringTranslation($this->translationManager);
     $operations = $list->getOperations($this->role);
     $this->assertInternalType('array', $operations);
     $this->assertArrayHasKey('edit', $operations);
     $this->assertInternalType('array', $operations['edit']);
     $this->assertArrayHasKey('title', $operations['edit']);
     $this->assertArrayHasKey('delete', $operations);
     $this->assertInternalType('array', $operations['delete']);
     $this->assertArrayHasKey('title', $operations['delete']);
     $this->assertArrayHasKey($operation_name, $operations);
     $this->assertInternalType('array', $operations[$operation_name]);
     $this->assertArrayHasKey('title', $operations[$operation_name]);
 }
Пример #3
0
 /**
  * Tests that buildRow() returns a string which has been run through
  * SafeMarkup::checkPlain().
  *
  * @dataProvider providerTestBuildRow
  *
  * @param string $input
  *  The entity label being passed into buildRow.
  * @param string $expected
  *  The expected output of the label from buildRow.
  * @param string $message
  *   The message to provide as output for the test.
  * @param bool $ignorewarnings
  *   Whether or not to ignore PHP 5.3+ invalid multibyte sequence warnings.
  *
  * @see \Drupal\Core\Entity\EntityListBuilder::buildRow()
  */
 public function testBuildRow($input, $expected, $message, $ignorewarnings = FALSE)
 {
     $this->role->expects($this->any())->method('label')->will($this->returnValue($input));
     if ($ignorewarnings) {
         $built_row = @$this->entityListBuilder->buildRow($this->role);
     } else {
         $built_row = $this->entityListBuilder->buildRow($this->role);
     }
     $this->assertEquals($built_row['label'], $expected, $message);
 }
Пример #4
0
 /**
  * Grant permissions to a user role.
  *
  * @param \Drupal\user\RoleInterface $role
  *   The ID of a user role to alter.
  * @param array $permissions
  *   (optional) A list of permission names to grant.
  */
 protected function grantPermissions(RoleInterface $role, array $permissions)
 {
     foreach ($permissions as $permission) {
         $role->grantPermission($permission);
     }
     $role->trustData()->save();
 }
 /**
  * {@inheritdoc}
  */
 protected function getRoles()
 {
     return array($this->userRole->id() => $this->userRole);
 }