/** * Tests entity operations field. */ public function testEntityOperations() { // Create some test entities. for ($i = 0; $i < 5; $i++) { EntityTest::create(array('name' => $this->randomString()))->save(); } $entities = EntityTest::loadMultiple(); $admin_user = $this->drupalCreateUser(array('access administration pages', 'administer entity_test content')); $this->drupalLogin($admin_user); $this->drupalGet('test-entity-operations'); foreach ($entities as $entity) { $operations = \Drupal::entityManager()->getListBuilder('entity_test')->getOperations($entity); foreach ($operations as $operation) { $expected_destination = Url::fromUri('internal:/test-entity-operations')->toString(); $result = $this->xpath('//ul[contains(@class, dropbutton)]/li/a[contains(@href, :path) and text()=:title]', array(':path' => $operation['url']->toString() . '?destination=' . $expected_destination, ':title' => $operation['title'])); $this->assertEqual(count($result), 1, t('Found entity @operation link with destination parameter.', array('@operation' => $operation['title']))); } } }
/** * Tests whether entity links behave as expected. * * @param \Drupal\Core\Session\AccountInterface $account * The user account to be used to run the test; * @param bool[] $expected_results * An associative array of expected results keyed by link template name. */ protected function doTestEntityLink(AccountInterface $account, $expected_results) { \Drupal::currentUser()->setAccount($account); $view = Views::getView('test_entity_test_link'); $view->preview(); $info = ['canonical' => ['label' => 'View entity test', 'field_id' => 'view_entity_test', 'destination' => FALSE], 'edit-form' => ['label' => 'Edit entity test', 'field_id' => 'edit_entity_test', 'destination' => TRUE], 'delete-form' => ['label' => 'Delete entity test', 'field_id' => 'delete_entity_test', 'destination' => TRUE]]; $index = 0; foreach (EntityTest::loadMultiple() as $entity) { foreach ($expected_results as $template => $expected_result) { $expected_link = ''; if ($expected_result) { $path = $entity->url($template); $destination = $info[$template]['destination'] ? '?destination=/' : ''; $expected_link = '<a href="' . $path . $destination . '" hreflang="en">' . $info[$template]['label'] . '</a>'; } $link = $view->style_plugin->getField($index, $info[$template]['field_id']); $this->assertEqual($link, $expected_link); } $index++; } }
/** * Tests valid and invalid create requests for 'entity_test' entity type. */ public function testCreateEntityTest() { $entity_type = 'entity_test'; // Enables the REST service for 'entity_test' entity type. $this->enableService('entity:' . $entity_type, 'POST'); // Create two accounts with the required permissions to create resources. // The second one has administrative permissions. $accounts = $this->createAccountPerEntity($entity_type); // Verify create requests per user. foreach ($accounts as $key => $account) { $this->drupalLogin($account); // Populate some entity properties before create the entity. $entity_values = $this->entityValues($entity_type); $entity = EntityTest::create($entity_values); // Serialize the entity before the POST request. $serialized = $this->serializer->serialize($entity, $this->defaultFormat, ['account' => $account]); // Create the entity over the REST API. $this->assertCreateEntityOverRestApi($entity_type, $serialized); // Get the entity ID from the location header and try to read it from the // database. $this->assertReadEntityIdFromHeaderAndDb($entity_type, $entity, $entity_values); // Try to create an entity with an access protected field. // @see entity_test_entity_field_access() $normalized = $this->serializer->normalize($entity, $this->defaultFormat, ['account' => $account]); $normalized['field_test_text'][0]['value'] = 'no access value'; $this->httpRequest('entity/' . $entity_type, 'POST', $this->serializer->serialize($normalized, $this->defaultFormat, ['account' => $account]), $this->defaultMimeType); $this->assertResponse(403); $this->assertFalse(EntityTest::loadMultiple(), 'No entity has been created in the database.'); // Try to create a field with a text format this user has no access to. $entity->field_test_text->value = $entity_values['field_test_text'][0]['value']; $entity->field_test_text->format = 'full_html'; $serialized = $this->serializer->serialize($entity, $this->defaultFormat, ['account' => $account]); $this->httpRequest('entity/' . $entity_type, 'POST', $serialized, $this->defaultMimeType); // The value selected is not a valid choice because the format must be // 'plain_txt'. $this->assertResponse(422); $this->assertFalse(EntityTest::loadMultiple(), 'No entity has been created in the database.'); // Restore the valid test value. $entity->field_test_text->format = 'plain_text'; $serialized = $this->serializer->serialize($entity, $this->defaultFormat, ['account' => $account]); // Try to send invalid data that cannot be correctly deserialized. $this->assertCreateEntityInvalidData($entity_type); // Try to send no data at all, which does not make sense on POST requests. $this->assertCreateEntityNoData($entity_type); // Try to send invalid data to trigger the entity validation constraints. // Send a UUID that is too long. $this->assertCreateEntityInvalidSerialized($entity, $entity_type); // Try to create an entity without proper permissions. $this->assertCreateEntityWithoutProperPermissions($entity_type, $serialized, ['account' => $account]); } }