/** * Tests creating/editing an Order. */ public function testCreateOrder() { // Create a order through the add form. $this->drupalGet('/admin/commerce/orders'); $this->clickLink('Create a new order'); $user = $this->loggedInUser->getAccountName() . ' (' . $this->loggedInUser->id() . ')'; $edit = ['customer_type' => 'existing', 'uid' => $user]; $this->drupalPostForm(NULL, $edit, t('Create')); // Check the integrity of the edit form. $this->assertResponse(200, 'The order edit form can be accessed.'); $this->assertFieldByName('billing_profile', NULL, 'Billing profile field is present'); $this->assertFieldByName('line_items[form][inline_entity_form][purchased_entity][0][target_id]', NULL, 'Purchased entity field is present'); $this->assertFieldByName('line_items[form][inline_entity_form][quantity][0][value]', NULL, 'Quantity field is present'); $this->assertFieldByName('line_items[form][inline_entity_form][unit_price][0][amount]', NULL, 'Unit price field is present'); $this->assertFieldsByValue(t('Create line item'), NULL, 'Create line item button is present'); $entity = $this->variation->getSku() . ' (' . $this->variation->id() . ')'; $edit = ['line_items[form][inline_entity_form][purchased_entity][0][target_id]' => $entity, 'line_items[form][inline_entity_form][quantity][0][value]' => 1, 'line_items[form][inline_entity_form][unit_price][0][amount]' => '9.99']; $this->drupalPostForm(NULL, $edit, t('Create line item')); $this->drupalPostForm(NULL, [], t('Edit')); $this->assertFieldByName('line_items[form][inline_entity_form][entities][0][form][purchased_entity][0][target_id]', NULL, 'SKU field is present'); $this->assertFieldByName('line_items[form][inline_entity_form][entities][0][form][quantity][0][value]', NULL, 'Price field is present'); $this->assertFieldByName('line_items[form][inline_entity_form][entities][0][form][unit_price][0][amount]', NULL, 'Status field is present'); $this->assertFieldsByValue(t('Update line item'), NULL, 'Update line item button is present'); $edit = ['line_items[form][inline_entity_form][entities][0][form][quantity][0][value]' => 3, 'line_items[form][inline_entity_form][entities][0][form][unit_price][0][amount]' => '1.11']; $this->drupalPostForm(NULL, $edit, t('Update line item')); $edit = ['billing_profile' => $this->billingProfile->id()]; $this->drupalPostForm(NULL, $edit, t('Save')); $order_number = $this->cssSelect('tr td.views-field-order-number'); $this->assertEqual(count($order_number), 1, 'Order exists in the table.'); }
/** * Tests creating a Order programaticaly and through the add form. */ public function testCreateOrder() { // Create a order through the add form. $this->drupalGet('/admin/commerce/orders'); $this->clickLink('Create a new order'); $user = $this->loggedInUser->getAccountName() . ' (' . $this->loggedInUser->id() . ')'; $values = ['customer_type' => 'existing', 'uid' => $user]; $this->drupalPostForm(NULL, $values, 'Create'); $entity = $this->variation->getSku() . ' (' . $this->variation->id() . ')'; $values = ['line_items[form][inline_entity_form][purchased_entity][0][target_id]' => $entity, 'line_items[form][inline_entity_form][quantity][0][value]' => 1, 'line_items[form][inline_entity_form][unit_price][0][amount]' => '9.99']; $this->drupalPostForm(NULL, $values, 'Create line item'); $values = ['billing_profile' => $this->billingProfile->id()]; $this->drupalPostForm(NULL, $values, t('Save')); $order_number = $this->cssSelect('tr td.views-field-order-number'); $this->assertEqual(count($order_number), 1, 'Order exists in the table.'); }
/** * Tests tabs in profile UI. */ public function testProfileTabs() { $types_data = ['profile_type_0' => ['label' => $this->randomMachineName()], 'profile_type_1' => ['label' => $this->randomMachineName()]]; /** @var ProfileType[] $types */ $types = []; foreach ($types_data as $id => $values) { $types[$id] = ProfileType::create(['id' => $id] + $values); $types[$id]->save(); } $this->container->get('router.builder')->rebuild(); $this->user1 = User::create(['name' => $this->randomMachineName(), 'mail' => $this->randomMachineName() . '@example.com']); $this->user1->save(); $this->user2 = User::create(['name' => $this->randomMachineName(), 'mail' => $this->randomMachineName() . '@example.com']); $this->user2->save(); // Create new profiles. $profile1 = Profile::create($expected = ['type' => $types['profile_type_0']->id(), 'uid' => $this->user1->id()]); $profile1->save(); $profile2 = Profile::create($expected = ['type' => $types['profile_type_1']->id(), 'uid' => $this->user2->id()]); $profile2->save(); $this->drupalLogin($this->adminUser); $this->drupalGet('admin/config'); $this->clickLink('User profiles'); $this->assertResponse(200); $this->assertUrl('admin/config/people/profiles'); $this->assertLink($profile1->label()); $this->assertLinkByHref($profile2->toUrl('canonical')->toString()); $tasks = [['entity.profile.collection', []], ['entity.profile_type.collection', []]]; $this->assertLocalTasks($tasks, 0); }
/** * @covers ::getState * @covers ::getOrderNumber * @covers ::setOrderNumber * @covers ::getEmail * @covers ::setEmail * @covers ::getIpAddress * @covers ::setIpAddress * @covers ::getLineItems * @covers ::setLineItems * @covers ::hasLineItems * @covers ::addLineItem * @covers ::removeLineItem * @covers ::hasLineItem * @covers ::getBillingProfile * @covers ::setBillingProfile * @covers ::getBillingProfileId * @covers ::setBillingProfileId * @covers ::getCreatedTime * @covers ::setCreatedTime */ public function testOrder() { // @todo Test the store and owner getters/setters as well. $profile = Profile::create(['type' => 'billing', 'address' => ['country' => 'FR', 'postal_code' => '75002', 'locality' => 'Paris', 'address_line1' => 'A french street', 'recipient' => 'John LeSmith']]); $profile->save(); $line_item = LineItem::create(['type' => 'test']); $line_item->save(); $order = Order::create(['type' => 'default', 'state' => 'completed', 'order_number' => '6', 'mail' => '*****@*****.**', 'ip_address' => '127.0.0.1', 'billing_profile' => $profile, 'line_items' => [$line_item]]); $order->save(); // getState() returns a StateItem. $this->assertEquals('completed', $order->getState()->value); $this->assertEquals('6', $order->getOrderNumber()); $order->setOrderNumber(7); $this->assertEquals('7', $order->getOrderNumber()); $this->assertEquals('*****@*****.**', $order->getEmail()); $order->setEmail('*****@*****.**'); $this->assertEquals('*****@*****.**', $order->getEmail()); $this->assertEquals('127.0.0.1', $order->getIpAddress()); $order->setIpAddress('127.0.0.2'); $this->assertEquals('127.0.0.2', $order->getIpAddress()); // Avoid passing an entire entity to assertEquals(), causes a crash. $profiles_match = $profile === $order->getBillingProfile(); $this->assertTrue($profiles_match); $this->assertEquals($profile->id(), $order->getBillingProfileId()); $another_profile = Profile::create(['type' => 'billing', 'address' => ['country' => 'FR', 'postal_code' => '75003', 'locality' => 'Paris', 'address_line1' => 'A different french street', 'recipient' => 'Pierre Bertrand']]); $another_profile->save(); $order->setBillingProfileId($another_profile->id()); $this->assertEquals($another_profile->id(), $order->getBillingProfileId()); $order->setBillingProfile($profile); $this->assertEquals($profile->id(), $order->getBillingProfileId()); // An initially saved line item won't be the same as the loaded one. $line_item = LineItem::load($line_item->id()); $line_items_match = [$line_item] == $order->getLineItems(); $this->assertTrue($line_items_match); $this->assertTrue($order->hasLineItem($line_item)); $order->removeLineItem($line_item); $this->assertFalse($order->hasLineItems()); $this->assertFalse($order->hasLineItem($line_item)); $order->addLineItem($line_item); $this->assertTrue($order->hasLineItem($line_item)); $another_line_item = LineItem::create(['type' => 'test']); $another_line_item->save(); $another_line_item = LineItem::load($another_line_item->id()); $new_line_items = [$line_item, $another_line_item]; $order->setLineItems($new_line_items); $line_items_match = $new_line_items == $order->getLineItems(); $this->assertTrue($line_items_match); $this->assertTrue($order->hasLineItems()); $order->setCreatedTime('635879700'); $this->assertEquals('635879700', $order->getCreatedTime('635879700')); }
/** * Tests loading default from storage handler. */ public function testLoadDefaultProfile() { $profile_type = $this->createProfileType('test_defaults', 'test_defaults'); // Create new profiles. $profile1 = Profile::create($expected = ['type' => $profile_type->id(), 'uid' => $this->user1->id()]); $profile1->setActive(TRUE); $profile1->save(); $profile2 = Profile::create($expected = ['type' => $profile_type->id(), 'uid' => $this->user1->id()]); $profile2->setActive(TRUE); $profile2->setDefault(TRUE); $profile2->save(); /** @var \Drupal\profile\ProfileStorageInterface $storage */ $storage = \Drupal::entityTypeManager()->getStorage('profile'); $default_profile = $storage->loadDefaultByUser($this->user1, $profile_type->id()); $this->assertEqual($profile2->id(), $default_profile->id()); }
/** * Tests CRUD operations. */ public function testCRUD() { $types_data = ['profile_type_0' => ['label' => $this->randomMachineName()], 'profile_type_1' => ['label' => $this->randomMachineName()]]; /** @var ProfileType[] $types */ $types = []; foreach ($types_data as $id => $values) { $types[$id] = ProfileType::create(['id' => $id] + $values); $types[$id]->save(); } $this->user1 = User::create(['name' => $this->randomMachineName(), 'mail' => $this->randomMachineName() . '@example.com']); $this->user1->save(); $this->user2 = User::create(['name' => $this->randomMachineName(), 'mail' => $this->randomMachineName() . '@example.com']); $this->user2->save(); $this->profileStorage = \Drupal::entityTypeManager()->getStorage('profile'); // Create a new profile. $profile = Profile::create($expected = ['type' => $types['profile_type_0']->id(), 'uid' => $this->user1->id()]); $this->assertIdentical($profile->id(), NULL); $this->assertTrue($profile->uuid()); $this->assertIdentical($profile->getType(), $expected['type']); $expected_label = t('@type profile of @username (uid: @uid)', ['@type' => $types['profile_type_0']->label(), '@username' => $this->user1->getDisplayName(), '@uid' => $this->user1->id()]); $this->assertEqual($profile->label(), $expected_label, new FormattableMarkup('Expected "%expected" but got "%got"', ['%expected' => $expected_label, '%got' => $profile->label()])); $this->assertIdentical($profile->getOwnerId(), $this->user1->id()); $this->assertIdentical($profile->getCreatedTime(), REQUEST_TIME); $this->assertIdentical($profile->getChangedTime(), REQUEST_TIME); // Save the profile. $status = $profile->save(); $this->assertIdentical($status, SAVED_NEW); $this->assertTrue($profile->id()); $this->assertIdentical($profile->getChangedTime(), REQUEST_TIME); // List profiles for the user and verify that the new profile appears. $list = $this->profileStorage->loadByProperties(['uid' => $this->user1->id()]); $list_ids = array_keys($list); $this->assertEqual($list_ids, [(int) $profile->id()]); // Reload and update the profile. /** @var Profile $profile */ $profile = Profile::load($profile->id()); $profile->setChangedTime($profile->getChangedTime() - 1000); $original = clone $profile; $status = $profile->save(); $this->assertIdentical($status, SAVED_UPDATED); $this->assertIdentical($profile->id(), $original->id()); $this->assertEqual($profile->getCreatedTime(), REQUEST_TIME); $this->assertEqual($original->getChangedTime(), REQUEST_TIME - 1000); // Changed time is only updated when saved through the UI form. // @see \Drupal\Core\Entity\ContentEntityForm::submitForm(). $this->assertEqual($profile->getChangedTime(), REQUEST_TIME - 1000); // Create a second profile. $user1_profile1 = $profile; $profile = Profile::create(['type' => $types['profile_type_0']->id(), 'uid' => $this->user1->id()]); $status = $profile->save(); $this->assertIdentical($status, SAVED_NEW); $user1_profile = $profile; // List profiles for the user and verify that both profiles appear. $list = $this->profileStorage->loadByProperties(['uid' => $this->user1->id()]); $list_ids = array_keys($list); $this->assertEqual($list_ids, [(int) $user1_profile1->id(), (int) $user1_profile->id()]); // Delete the second profile and verify that the first still exists. $user1_profile->delete(); $this->assertFalse(Profile::load($user1_profile->id())); $list = $this->profileStorage->loadByProperties(['uid' => $this->user1->id()]); $list_ids = array_keys($list); $this->assertEqual($list_ids, [(int) $user1_profile1->id()]); // Create a new second profile. $user1_profile = Profile::create(['type' => $types['profile_type_1']->id(), 'uid' => $this->user1->id()]); $status = $user1_profile->save(); $this->assertIdentical($status, SAVED_NEW); // Create a profile for the second user. $user2_profile1 = Profile::create(['type' => $types['profile_type_0']->id(), 'uid' => $this->user2->id()]); $status = $user2_profile1->save(); $this->assertIdentical($status, SAVED_NEW); // Delete the first user and verify that all of its profiles are deleted. $this->user1->delete(); $this->assertFalse(User::load($this->user1->id())); $list = $this->profileStorage->loadByProperties(['uid' => $this->user1->id()]); $list_ids = array_keys($list); $this->assertEqual($list_ids, []); // List profiles for the second user and verify that they still exist. $list = $this->profileStorage->loadByProperties(['uid' => $this->user2->id()]); $list_ids = array_keys($list); $this->assertEqual($list_ids, [(int) $user2_profile1->id()]); // @todo Rename a profile type; verify that existing profiles are updated. }
/** * Provides profile delete form. * * @param \Drupal\Core\Routing\RouteMatchInterface $route_match * The route match. * @param \Drupal\user\UserInterface $user * The user account. * @param \Drupal\profile\Entity\ProfileTypeInterface $profile_type * The profile type entity for the profile. * @param int $id * The id of the profile to delete. * * @return array * Returns form array. */ public function deleteProfile(RouteMatchInterface $route_match, UserInterface $user, ProfileTypeInterface $profile_type, $id) { return $this->entityFormBuilder()->getForm(Profile::load($id), 'delete'); }
/** * Tests CRUD operations for profile types through the UI. */ public function testCRUDUI() { $types_data = ['profile_type_0' => ['label' => $this->randomMachineName()], 'profile_type_1' => ['label' => $this->randomMachineName()]]; /** @var ProfileType[] $types */ $types = []; foreach ($types_data as $id => $values) { $types[$id] = $this->createProfileType($id, $values['label']); } $this->user1 = User::create(['name' => $this->randomMachineName(), 'mail' => $this->randomMachineName() . '@example.com']); $this->user1->save(); $this->user2 = User::create(['name' => $this->randomMachineName(), 'mail' => $this->randomMachineName() . '@example.com']); $this->user2->save(); // Create new profiles. $profile1 = Profile::create($expected = ['type' => $types['profile_type_0']->id(), 'uid' => $this->user1->id()]); $profile1->save(); $profile2 = Profile::create($expected = ['type' => $types['profile_type_1']->id(), 'uid' => $this->user2->id()]); $profile2->save(); $this->drupalLogin($this->adminUser); $this->drupalGet('admin/config'); $this->clickLink('User profiles'); $this->assertResponse(200); $this->assertUrl('admin/config/people/profiles'); $this->assertLink($profile1->label()); $this->assertLinkByHref($profile2->toUrl('canonical')->toString()); }
/** * Tests mark as default action. */ public function testDefaultAction() { $types_data = ['profile_type_0' => ['label' => $this->randomMachineName(), 'multiple' => TRUE], 'profile_type_1' => ['label' => $this->randomMachineName(), 'multiple' => TRUE]]; /** @var ProfileType[] $types */ $types = []; foreach ($types_data as $id => $values) { $types[$id] = $this->createProfileType($id, $values['label']); } $restricted_user = $this->drupalCreateUser(['administer profiles', 'edit own ' . $types['profile_type_0']->id() . ' profile', 'edit own ' . $types['profile_type_1']->id() . ' profile']); $admin_user = $this->drupalCreateUser(['administer profiles', 'edit any ' . $types['profile_type_0']->id() . ' profile', 'edit any ' . $types['profile_type_1']->id() . ' profile']); // Create new profiles. $profile_profile_type_0_restricted_user = Profile::create($expected = ['type' => $types['profile_type_0']->id(), 'uid' => $restricted_user->id()]); $profile_profile_type_0_restricted_user->setActive(TRUE); $profile_profile_type_0_restricted_user->save(); $profile_profile_type_0_user1_1 = Profile::create($expected = ['type' => $types['profile_type_0']->id(), 'uid' => $this->user1->id()]); $profile_profile_type_0_user1_1->setActive(TRUE); $profile_profile_type_0_user1_1->save(); $profile_profile_type_0_user1_2 = Profile::create($expected = ['type' => $types['profile_type_0']->id(), 'uid' => $this->user1->id()]); $profile_profile_type_0_user1_2->setActive(TRUE); $profile_profile_type_0_user1_2->save(); $profile_profile_type_1_user1 = Profile::create($expected = ['type' => $types['profile_type_1']->id(), 'uid' => $this->user1->id()]); $profile_profile_type_1_user1->setActive(TRUE); $profile_profile_type_1_user1->save(); $profile_profile_type_1_user2 = Profile::create($expected = ['type' => $types['profile_type_1']->id(), 'uid' => $this->user2->id()]); $profile_profile_type_1_user2->setActive(TRUE); $profile_profile_type_1_user2->save(); $profile_profile_type_1_user1_inactive = Profile::create($expected = ['type' => $types['profile_type_1']->id(), 'uid' => $this->user1->id()]); $profile_profile_type_1_user1_inactive->setActive(FALSE); $profile_profile_type_1_user1_inactive->save(); $profile_profile_type_1_user1_active = Profile::create($expected = ['type' => $types['profile_type_1']->id(), 'uid' => $this->user1->id()]); $profile_profile_type_1_user1_active->isActive(TRUE); $profile_profile_type_1_user1_active->save(); // Make sure that $restricted_user is allowed to set default his own profile // and not others'. $this->drupalLogin($restricted_user); $this->drupalGet('admin/config/people/profiles'); $this->clickLink('Mark as default', 0); $this->assertTrue(Profile::load($profile_profile_type_0_restricted_user->id())->isDefault()); $this->clickLink('Mark as default', 1); $this->assertResponse(403); $this->drupalLogout(); $this->drupalLogin($admin_user); $this->drupalGet('admin/config/people/profiles'); // Mark $profile_profile_type_0_user1_1 as default // $profile_profile_type_0_user1_2 should stay not default. $this->clickLink('Mark as default', 0); $this->assertTrue(Profile::load($profile_profile_type_0_user1_1->id())->isDefault()); $this->assertFalse($profile_profile_type_0_user1_2->isDefault()); // Mark $profile_profile_type_0_user1_2 as default // $profile_profile_type_0_user1_1 should become not default. $profile_profile_type_0_user1_2->setDefault(TRUE); $profile_profile_type_0_user1_2->save(); $this->assertTrue($profile_profile_type_0_user1_2->isDefault()); $this->assertFalse($profile_profile_type_0_user1_1->isDefault()); // Mark $profile_profile_type_1_user1 as default // $profile_profile_type_1_user2 should stay not default. $this->clickLink('Mark as default', 1); $this->assertTrue(Profile::load($profile_profile_type_1_user1->id())->isDefault()); $this->assertFalse($profile_profile_type_1_user2->isDefault()); // Mark $profile_profile_type_1_user2 as default // $profile_profile_type_1_user1 should stay default. $profile_profile_type_1_user2->setDefault(TRUE); $profile_profile_type_1_user2->save(); $this->assertTrue($profile_profile_type_1_user2->isDefault()); $this->assertTrue(Profile::load($profile_profile_type_1_user1->id())->isDefault()); // Mark $profile_profile_type_1_user1_inactive as default // $profile_profile_type_1_user1_active should stay not default. $this->clickLink('Mark as default', 2); $this->assertTrue(Profile::load($profile_profile_type_1_user1_inactive->id())->isDefault()); $this->assertFalse($profile_profile_type_1_user1_active->isDefault()); // Mark $profile_profile_type_1_user1_active as default // $profile_profile_type_1_user1_inactive should stay default. $profile_profile_type_1_user1_active->setDefault(TRUE); $profile_profile_type_1_user1_active->save(); $this->assertTrue($profile_profile_type_1_user1_active->isDefault()); $this->assertTrue(Profile::load($profile_profile_type_1_user1_inactive->id())->isDefault()); }
/** * Tests profile edit flow and permissions. */ public function testProfileEditAccess() { // Setup users. $web_user1 = $this->drupalCreateUser(["add own {$this->type->id()} profile", "edit own {$this->type->id()} profile"]); $web_user2 = $this->drupalCreateUser(["add any {$this->type->id()} profile", "edit any {$this->type->id()} profile"]); // Setup profiles. $profile1 = Profile::create(['uid' => $web_user1->id(), 'type' => $this->type->id()]); $profile1->set($this->field->getName(), $this->randomString()); $profile1->save(); $profile2 = Profile::create(['uid' => $web_user2->id(), 'type' => $this->type->id()]); $profile2->set($this->field->getName(), $this->randomString()); $profile2->save(); // Test user1 can only edit own profiles. $access = $profile1->access('edit', $web_user1); $this->assertTrue($access); $access = $profile2->access('edit', $web_user1); $this->assertFalse($access); // Test user2 can edit any profiles. $access = $profile1->access('edit', $web_user2); $this->assertTrue($access); $access = $profile2->access('edit', $web_user2); $this->assertTrue($access); }
/** * Create a user, and optionally a profile. * * @param \Drupal\profile\Entity\ProfileTypeInterface $profile_type * A profile type for the created profile entity. * @param \Drupal\user\UserInterface $user * A user to create a profile. * * @return \Drupal\profile\Entity\ProfileInterface * A profile for a user. */ protected function createProfile(ProfileTypeInterface $profile_type, UserInterface $user) { $profile = Profile::create(['type' => $profile_type->id(), 'uid' => $user->id()]); $profile->save(); return $profile; }