View source
<?php
use Drupal\node\Entity\Node;
use Drupal\user\Entity\Role;
use Drupal\user\Entity\User;
use Drupal\consumers\Entity\Consumer;
function api_test_install() {
$permissions = [
"access content",
"access contextual links",
"access toolbar",
"bypass node access",
"create page content",
"create url aliases",
"delete page revisions",
"edit any page content",
"edit own page content",
"revert page revisions",
"use text format rich_text",
"view all revisions",
"view own unpublished content",
"view page revisions",
];
$role = Role::create([
'id' => 'test-role-page_creator',
'label' => 'Test Role Page Creator',
]);
$role
->save();
user_role_grant_permissions($role
->id(), $permissions);
$edit = [
'name' => 'api-test-user',
'mail' => 'api-test-user@example.com',
'pass' => 'admin',
];
$account = User::create($edit);
$account
->addRole('test-role-page_creator');
$account
->activate();
$account
->save();
$api_test_user_id = $account
->id();
$data = [
'uuid' => 'api_test-oauth2-client',
'label' => 'API Test Client',
'secret' => 'oursecret',
'confidential' => 1,
'user_id' => $api_test_user_id,
'roles' => 'test-role-page_creator',
];
$client = Consumer::create($data);
$client
->save();
$data = [
'title' => 'Unpublished Page',
'type' => 'page',
'uuid' => 'api_test-unpublished-page-content',
'body' => '--TESTING--',
'status' => FALSE,
];
$node = Node::create($data);
$node
->save();
$data = [
'title' => 'Published Page',
'type' => 'page',
'uuid' => 'api_test-published-page-content',
'body' => '--TESTING--',
'status' => TRUE,
];
$node = Node::create($data);
$node
->save();
}
function api_test_uninstall() {
$username = 'api-test-user';
$storage = \Drupal::entityTypeManager()
->getStorage('user');
$users = $storage
->loadByProperties([
'name' => $username,
]);
$storage
->delete($users);
$client_uuid = 'api_test-oauth2-client';
$storage = \Drupal::entityTypeManager()
->getStorage('consumer');
$clients = $storage
->loadByProperties([
'uuid' => $client_uuid,
]);
$storage
->delete($clients);
$storage = \Drupal::entityTypeManager()
->getStorage('node');
$nodes = $storage
->loadByProperties([
'body' => '--TESTING--',
]);
$storage
->delete($nodes);
}