/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->installSchema('system', 'sequences'); $this->installEntitySchema('user'); $this->manager = $this->container->get('plugin.manager.condition'); // Set up the authenticated and anonymous roles. Role::create(array('id' => DRUPAL_ANONYMOUS_RID, 'label' => 'Anonymous user'))->save(); Role::create(array('id' => DRUPAL_AUTHENTICATED_RID, 'label' => 'Authenticated user'))->save(); // Create new role. $rid = strtolower($this->randomMachineName(8)); $label = $this->randomString(8); $role = Role::create(array('id' => $rid, 'label' => $label)); $role->save(); $this->role = $role; // Setup an anonymous user for our tests. $this->anonymous = User::create(array('uid' => 0)); $this->anonymous->save(); // Loading the anonymous user adds the correct role. $this->anonymous = User::load($this->anonymous->id()); // Setup an authenticated user for our tests. $this->authenticated = User::create(array('name' => $this->randomMachineName())); $this->authenticated->save(); // Add the custom role. $this->authenticated->addRole($this->role->id()); }
/** * Tests switch user. */ public function testSwitchUser() { $this->drupalLogin($this->webUser); $this->drupalGet(''); $this->assertNoText($this->block->label(), 'Block title was not found.'); // Ensure that a token is required to switch user. $this->drupalGet('/devel/switch/' . $this->webUser->getUsername()); $this->assertResponse(403); $this->drupalLogin($this->develUser); $this->drupalGet(''); $this->assertText($this->block->label(), 'Block title was found.'); // Ensure that if name in not passed the controller returns access denied. $this->drupalGet('/devel/switch'); $this->assertResponse(403); // Ensure that a token is required to switch user. $this->drupalGet('/devel/switch/' . $this->switchUser->getUsername()); $this->assertResponse(403); // Switch to another user account. $this->drupalGet('/user/' . $this->switchUser->id()); $this->clickLink($this->switchUser->getUsername()); $this->assertSessionByUid($this->switchUser->id()); $this->assertNoSessionByUid($this->develUser->id()); // Switch back to initial account. $this->clickLink($this->develUser->getUsername()); $this->assertNoSessionByUid($this->switchUser->id()); $this->assertSessionByUid($this->develUser->id()); // Use the search form to switch to another account. $edit = ['userid' => $this->switchUser->getUsername()]; $this->drupalPostForm(NULL, $edit, t('Switch')); $this->assertSessionByUid($this->switchUser->id()); $this->assertNoSessionByUid($this->develUser->id()); }
/** * Checks whether skip() marks the check as skipped, and checks the * skippedBy() value. */ public function testSkipCheck() { foreach ($this->checks as $check) { $check->skip(); $isSkipped = $check->isSkipped(); $skippedBy = $check->skippedBy(); $this->assertTrue($isSkipped, $check->getTitle() . ' skipped.'); $this->assertEqual($this->user->id(), $skippedBy->id(), 'Skipped by ' . $skippedBy->label()); } }
/** * Creates a test entity. * * @param string $entity_type * An entity type. * * @return \Drupal\Core\Entity\EntityInterface * The created test entity. */ protected function createTestEntity($entity_type) { $this->entity_name = $this->randomMachineName(); $this->entity_user = $this->createUser(); $this->entity_field_text = $this->randomMachineName(); // Pass in the value of the name field when creating. With the user // field we test setting a field after creation. $entity = entity_create($entity_type); $entity->user_id->target_id = $this->entity_user->id(); $entity->name->value = $this->entity_name; // Set a value for the test field. $entity->field_test_text->value = $this->entity_field_text; return $entity; }
/** * Test registered Serializer's entity serialization for core's formats. */ public function testSerialize() { // Test that Serializer responds using the ComplexDataNormalizer and // JsonEncoder. The output of ComplexDataNormalizer::normalize() is tested // elsewhere, so we can just assume that it works properly here. $normalized = $this->serializer->normalize($this->entity, 'json'); $expected = json_encode($normalized); // Test 'json'. $actual = $this->serializer->serialize($this->entity, 'json'); $this->assertIdentical($actual, $expected, 'Entity serializes to JSON when "json" is requested.'); $actual = $this->serializer->serialize($normalized, 'json'); $this->assertIdentical($actual, $expected, 'A normalized array serializes to JSON when "json" is requested'); // Test 'ajax'. $actual = $this->serializer->serialize($this->entity, 'ajax'); $this->assertIdentical($actual, $expected, 'Entity serializes to JSON when "ajax" is requested.'); $actual = $this->serializer->serialize($normalized, 'ajax'); $this->assertIdentical($actual, $expected, 'A normalized array serializes to JSON when "ajax" is requested'); // Generate the expected xml in a way that allows changes to entity property // order. $expected = array('id' => '<id><value>' . $this->entity->id() . '</value></id>', 'uuid' => '<uuid><value>' . $this->entity->uuid() . '</value></uuid>', 'langcode' => '<langcode><value>en</value></langcode>', 'name' => '<name><value>' . $this->values['name'] . '</value></name>', 'type' => '<type><value>entity_test_mulrev</value></type>', 'created' => '<created><value>' . $this->entity->created->value . '</value></created>', 'user_id' => '<user_id><target_id>' . $this->user->id() . '</target_id><url>' . $this->user->url() . '</url></user_id>', 'revision_id' => '<revision_id><value>' . $this->entity->getRevisionId() . '</value></revision_id>', 'default_langcode' => '<default_langcode><value>1</value></default_langcode>', 'field_test_text' => '<field_test_text><value>' . $this->values['field_test_text']['value'] . '</value><format>' . $this->values['field_test_text']['format'] . '</format></field_test_text>'); // Sort it in the same order as normalised. $expected = array_merge($normalized, $expected); // Add header and footer. array_unshift($expected, '<?xml version="1.0"?>' . PHP_EOL . '<response>'); $expected[] = '</response>' . PHP_EOL; // Reduced the array to a string. $expected = implode('', $expected); // Test 'xml'. The output should match that of Symfony's XmlEncoder. $actual = $this->serializer->serialize($this->entity, 'xml'); $this->assertIdentical($actual, $expected); $actual = $this->serializer->serialize($normalized, 'xml'); $this->assertIdentical($actual, $expected); }
/** * Checks the session data returned by the session test routes. * * @param string $response * A response object containing the session values and the user ID. * @param string $expected * The expected session value. */ protected function assertSessionData($response, $expected) { $response = json_decode($response, TRUE); $this->assertEqual(['test_value' => $expected], $response['session'], 'The session data matches the expected value.'); // Check that we are logged in as the correct user. $this->assertEqual($this->user->id(), $response['user'], 'The correct user is logged in.'); }
/** * Creates a test entity. * * @param string $entity_type * An entity type. * * @return \Drupal\Core\Entity\EntityInterface * The created test entity. */ protected function createTestEntity($entity_type) { $this->entityName = $this->randomMachineName(); $this->entityUser = $this->createUser(); // Pass in the value of the name field when creating. With the user // field we test setting a field after creation. $entity = $this->container->get('entity_type.manager')->getStorage($entity_type)->create(); $entity->user_id->target_id = $this->entityUser->id(); $entity->name->value = $this->entityName; // Set a value for the test field. if ($entity->hasField('field_test_text')) { $this->entityFieldText = $this->randomMachineName(); $entity->field_test_text->value = $this->entityFieldText; } return $entity; }
/** * Tests the actual language negotiation. */ function testActualNegotiation() { $this->drupalLogin($this->adminUser); $this->addCustomLanguage(); $this->setLanguageNegotiation(); // Even though we have admin language negotiation, so long as the user has // no preference set, negotiation will fall back further. $path = 'user/' . $this->adminUser->id() . '/edit'; $this->drupalGet($path); $this->assertText('Language negotiation method: language-default'); // Set a preferred language code for the user. $path = 'user/' . $this->adminUser->id() . '/edit'; $edit = array(); $edit['preferred_admin_langcode'] = 'xx'; $this->drupalPostForm($path, $edit, t('Save')); // Test negotiation with the URL method first. The admin method will only // be used if the URL method did not match. $path = 'user/' . $this->adminUser->id() . '/edit'; $this->drupalGet($path); $this->assertText('Language negotiation method: language-user-admin'); $path = 'xx/user/' . $this->adminUser->id() . '/edit'; $this->drupalGet($path); $this->assertText('Language negotiation method: language-url'); // Test negotiation with the admin language method first. The admin method // will be used at all times. $this->setLanguageNegotiation(TRUE); $path = 'user/' . $this->adminUser->id() . '/edit'; $this->drupalGet($path); $this->assertText('Language negotiation method: language-user-admin'); $path = 'xx/user/' . $this->adminUser->id() . '/edit'; $this->drupalGet($path); $this->assertText('Language negotiation method: language-user-admin'); }
/** * Helper function to set up a new comment type and add it to a node. * * @param string $comment_type_id * The id to use for the new comment type. * @param string bundle * A node bundle to create. * @returns \Drupal\node\NodeInterface * A new node of the specified bundle with the comment type field. */ protected function setupAlternateComments($comment_type_id, $bundle) { $this->createCommentType($comment_type_id); $this->drupalCreateContentType(['type' => $bundle, 'name' => 'Page']); $this->addCommentsToNode($bundle, DRUPAL_OPTIONAL, $comment_type_id); return $this->drupalCreateNode(['type' => $bundle, 'uid' => $this->webUser->id()]); }
/** * Helper function, which creates node of type person * * @param \Drupal\user\Entity\User $user * @param bool $isIndividualSponsor */ public function createUserProfile(User $user, $isIndividualSponsor = FALSE) { $values = ['type' => 'person', 'title' => $user->getUsername(), 'uid' => 1, 'field_referenced_user' => ['target_id' => $user->id()]]; if ($isIndividualSponsor) { $values['field_person_type'] = ['target_id' => 8]; } $node = entity_create('node', $values); $node->save(); }
/** * Provide a list of tokens. */ public function tokenList(User $user) { $entity_type = 'access_token'; $storage = $this->entityManager()->getStorage($entity_type); $ids = $storage->getQuery()->condition('auth_user_id', $user->id())->execute(); if (empty($ids)) { return ['#markup' => $this->t('There are no tokens for this user.')]; } $view_controller = $this->entityManager()->getViewBuilder($entity_type); $tokens = $storage->loadMultiple($ids); return $view_controller->viewMultiple($tokens); }
public function testAuthenticatedCheckout() { $this->drupalLogin($this->customer); $this->addToCart($this->product); $order = $this->checkout(); $this->assertRaw('Your order is complete!'); $this->assertRaw('While logged in'); $this->assertEqual($order->getOwnerId(), $this->customer->id(), 'Order has the correct user ID.'); $this->assertEqual($order->getEmail(), $this->customer->getEmail(), 'Order has the correct email address.'); // Check that cart is now empty. $this->drupalGet('cart'); $this->assertText('There are no products in your shopping cart.'); }
/** * Check that a basic authentication session does not leak. * * Regression test for a bug that caused a session initiated by basic * authentication to persist over subsequent unauthorized requests. */ public function testSessionFromBasicAuthenticationDoesNotLeak() { // This route is authorized through basic_auth only, not cookie. $protected_url = Url::fromRoute('session_test.get_session_basic_auth'); // This route is not protected. $unprotected_url = Url::fromRoute('session_test.get_session_no_auth'); // Test that the route is not accessible as an anonymous user. $this->drupalGet($protected_url); $this->assertResponse(401, 'An anonymous user cannot access a route protected with basic authentication.'); // We should be able to access the route with basic authentication. $this->basicAuthGet($protected_url, $this->user->getUsername(), $this->user->pass_raw); $this->assertResponse(200, 'A route protected with basic authentication can be accessed by an authenticated user.'); // Check that the correct user is logged in. $this->assertEqual($this->user->id(), json_decode($this->getRawContent())->user, 'The correct user is authenticated on a route with basic authentication.'); // If we now try to access a page without basic authentication then we // should no longer be logged in. $this->drupalGet($unprotected_url); $this->assertResponse(200, 'An unprotected route can be accessed without basic authentication.'); $this->assertFalse(json_decode($this->getRawContent())->user, 'The user is no longer authenticated after visiting a page without basic authentication.'); // If we access the protected page again without basic authentication we // should get 401 Unauthorized. $this->drupalGet($protected_url); $this->assertResponse(401, 'A subsequent request to the same route without basic authentication is not authorized.'); }
/** * Executes the save tests for the given entity type. * * @param string $entity_type * The entity type to run the tests with. */ protected function assertSave($entity_type) { $entity = $this->createTestEntity($entity_type); $entity->save(); $this->assertTrue((bool) $entity->id(), format_string('%entity_type: Entity has received an id.', array('%entity_type' => $entity_type))); $entity = entity_load($entity_type, $entity->id()); $this->assertTrue((bool) $entity->id(), format_string('%entity_type: Entity loaded.', array('%entity_type' => $entity_type))); // Access the name field. $this->assertEqual(1, $entity->id->value, format_string('%entity_type: ID value can be read.', array('%entity_type' => $entity_type))); $this->assertTrue(is_string($entity->uuid->value), format_string('%entity_type: UUID value can be read.', array('%entity_type' => $entity_type))); $this->assertEqual('en', $entity->langcode->value, format_string('%entity_type: Language code can be read.', array('%entity_type' => $entity_type))); $this->assertEqual(\Drupal::languageManager()->getLanguage('en'), $entity->langcode->language, format_string('%entity_type: Language object can be read.', array('%entity_type' => $entity_type))); $this->assertEqual($this->entity_user->id(), $entity->user_id->target_id, format_string('%entity_type: User id can be read.', array('%entity_type' => $entity_type))); $this->assertEqual($this->entity_user->getUsername(), $entity->user_id->entity->name->value, format_string('%entity_type: User name can be read.', array('%entity_type' => $entity_type))); $this->assertEqual($this->entity_field_text, $entity->field_test_text->value, format_string('%entity_type: Text field can be read.', array('%entity_type' => $entity_type))); }
/** * Tests that the admin language is configurable only for administrators. * * If a user has the permission "access administration pages", they should * be able to see the setting to pick the language they want those pages in. * * If a user does not have that permission, it would confusing for them to * have a setting for pages they cannot access, so they should not be able to * set a language for those pages. */ function testUserAdminLanguageConfigurationAvailableIfAdminLanguageNegotiationIsEnabled() { $this->drupalLogin($this->adminUser); // Adds a new language, because with only one language, setting won't show. $this->addCustomLanguage(); $this->setLanguageNegotiation(); $path = 'user/' . $this->adminUser->id() . '/edit'; $this->drupalGet($path); // Ensure administration pages language setting is visible for admin. $this->assertFieldByXPath($this->constructFieldXpath('id', 'edit-preferred-admin-langcode'), NULL, 'Administration pages language selector available for admins.'); // Ensure administration pages language setting is hidden for non-admins. $this->drupalLogin($this->regularUser); $path = 'user/' . $this->regularUser->id() . '/edit'; $this->drupalGet($path); $this->assertNoFieldByXPath($this->constructFieldXpath('id', 'edit-preferred-admin-langcode'), NULL, 'Administration pages language selector not available for regular user.'); }