View source
<?php
namespace Drupal\social\Behat;
use Drupal\advancedqueue\Annotation\AdvancedQueueJobType;
use Drupal\advancedqueue\Commands\AdvancedQueueCommands;
use Drupal\DrupalExtension\Context\DrupalContext;
use Drupal\user\Entity\User;
use Drupal\big_pipe\Render\Placeholder\BigPipeStrategy;
use Behat\Mink\Exception\UnsupportedDriverActionException;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Gherkin\Node\TableNode;
use Drupal\DrupalExtension\Hook\Scope\EntityScope;
class SocialDrupalContext extends DrupalContext {
public function prepareBigPipeNoJsCookie(BeforeScenarioScope $scope) {
if (!$this
->getSession()
->isStarted()) {
$this
->getSession()
->start();
}
try {
$this
->getSession()
->getDriver()
->executeScript('true');
} catch (UnsupportedDriverActionException $e) {
if ($this
->getSession()) {
$this
->getSession()
->setCookie(BigPipeStrategy::NOJS_COOKIE, TRUE);
}
} catch (\Exception $e) {
}
}
public function beforeUserCreateObject(EntityScope $scope) {
$user = $scope
->getEntity();
if (!isset($user->mail)) {
$user->mail = strtolower(trim($user->name)) . '@example.com';
}
}
public function bootstrapWithAdminUser(BeforeScenarioScope $scope) {
$admin_user = User::load('1');
$current_user = \Drupal::getContainer()
->get('current_user');
$current_user
->setAccount($admin_user);
}
public function assertViewingMyNode($type, TableNode $fields) {
$user_manager = $this
->getUserManager();
$user = $user_manager
->getCurrentUser();
if (!$user) {
throw new \Exception(sprintf('There is no current logged in user to create a node for.'));
}
$node = (object) array(
'type' => $type,
);
foreach ($fields
->getRowsHash() as $field => $value) {
if (strpos($field, 'date') !== FALSE) {
$value = date('Y-m-d H:i:s', strtotime($value));
}
$node->{$field} = $value;
}
$node->uid = $user->uid;
$saved = $this
->nodeCreate($node);
$this
->getSession()
->visit($this
->locatePath('/node/' . $saved->nid));
}
public function assertViewingNode($type, TableNode $fields) {
$node = (object) array(
'type' => $type,
);
foreach ($fields
->getRowsHash() as $field => $value) {
if (strpos($field, 'date') !== FALSE) {
$value = date('Y-m-d H:i:s', strtotime($value));
}
$node->{$field} = $value;
}
$saved = $this
->nodeCreate($node);
$this
->getSession()
->visit($this
->locatePath('/node/' . $saved->nid));
}
public function createNodes($type, TableNode $nodesTable) {
foreach ($nodesTable
->getHash() as $nodeHash) {
$node = (object) $nodeHash;
$node->type = $type;
if (isset($node->field_event_date)) {
$node->field_event_date = date('Y-m-d H:i:s', strtotime($node->field_event_date));
}
$entity = $this
->nodeCreate($node);
if (isset($node->alias)) {
\Drupal::service('path.alias_storage')
->save("/node/" . $entity->nid, $node->alias);
}
}
}
public function createTopics($count, $title, $username) {
$accounts = \Drupal::entityTypeManager()
->getStorage('user')
->loadByProperties([
'name' => $username,
]);
if (!$accounts) {
return;
}
$account = reset($accounts);
for ($index = 1; $index <= $count; $index++) {
$node = (object) [
'type' => 'topic',
'title' => str_replace('[id]', $index, $title),
'uid' => $account
->id(),
];
$this
->nodeCreate($node);
}
}
public function createComments($count, $text, $topic) {
$nodes = \Drupal::entityTypeManager()
->getStorage('node')
->loadByProperties([
'title' => $topic,
]);
if (!$nodes) {
return;
}
$node = reset($nodes);
if ($node
->bundle() !== 'topic') {
return;
}
$storage = \Drupal::entityTypeManager()
->getStorage('comment');
for ($index = 1; $index <= $count; $index++) {
$storage
->create([
'entity_id' => $node
->id(),
'entity_type' => $node
->getEntityTypeId(),
'field_name' => 'field_topic_comments',
'field_comment_body' => str_replace('[id]', $index, $text),
'uid' => $node
->getOwnerId(),
])
->save();
}
}
public function updateSearchIndexes() {
$index_storage = \Drupal::service("entity_type.manager")
->getStorage('search_api_index');
$indexes = $index_storage
->loadMultiple();
if (!$indexes) {
return;
}
foreach ($indexes as $index) {
$index
->indexItems();
}
}
public function iEmptyTheQueue() {
$this
->processQueue(TRUE);
}
public function iWaitForTheQueueToBeEmpty() {
$this
->processQueue();
}
public function iCheckIFQueueItemsProcessed($item_name = "") {
$query = \Drupal::database()
->select('queue', 'q');
$query
->addField('q', 'item_id');
$query
->condition('q.name', $item_name);
$item = $query
->execute()
->fetchField();
if (!empty($item)) {
throw new \Exception('There are exist stuck items in queue.');
}
}
protected function processQueue($just_delete = FALSE) {
$workerManager = \Drupal::service('plugin.manager.queue_worker');
$queue = \Drupal::service('queue');
for ($i = 0; $i < 20; $i++) {
foreach ($workerManager
->getDefinitions() as $name => $info) {
$worker = $queue
->get($name);
$queue_worker = $workerManager
->createInstance($name);
if ($worker
->numberOfItems() > 0) {
while ($item = $worker
->claimItem()) {
if ($just_delete === FALSE) {
$queue_worker
->processItem($item->data);
}
$worker
->deleteItem($item);
}
}
}
}
if (\Drupal::moduleHandler()
->moduleExists('advancedqueue')) {
$queue_storage = \Drupal::service("entity_type.manager")
->getStorage('advancedqueue_queue');
$queue = $queue_storage
->load('default');
$processor = \Drupal::service('advancedqueue.processor');
$processor
->processQueue($queue);
}
}
public function iResetTour($tour_id) {
$query = \Drupal::database()
->delete('users_data');
$query
->condition('module', 'social_tour');
$query
->condition('name', 'social-home');
$query
->execute();
}
public function iWaitForSeconds($seconds, $condition = "") {
$milliseconds = (int) ($seconds * 1000);
$this
->getSession()
->wait($milliseconds, $condition);
}
public function iEnableTheModule($module_name) {
$modules = [
$module_name,
];
\Drupal::service('module_installer')
->install($modules);
}
public function iDisableTheModule($module_name) {
$modules = [
$module_name,
];
\Drupal::service('module_installer')
->uninstall($modules);
}
public function iEnableTheTourSetting() {
\Drupal::configFactory()
->getEditable('social_tour.settings')
->set('social_tour_enabled', 1)
->save();
}
public function iEnableNicknameField() {
if (!\Drupal::service('module_handler')
->moduleExists("social_profile_fields")) {
throw new \Exception("Could not enable nickname field for profile because the Social Profile Fields module is disabled.");
}
\Drupal::configFactory()
->getEditable('social_profile_fields.settings')
->set("profile_profile_field_profile_nick_name", TRUE)
->save();
}
public function iRestrictRealNameUsage($restrict = TRUE) {
if (!\Drupal::service('module_handler')
->moduleExists("social_profile_privacy")) {
throw new \Exception("Could not restrict real name usage because the Social Profile Privacy module is disabled.");
}
if ($restrict === "un") {
$restrict = FALSE;
}
if ($restrict !== FALSE && $restrict !== TRUE) {
throw new \Exception("Restrict has unknown value " . print_r($restrict, true));
}
\Drupal::configFactory()
->getEditable('social_profile_privacy.settings')
->set("limit_search_and_mention", $restrict)
->save();
}
public function iSearchIndexForTerm($index, $term) {
$this
->getSession()
->visit($this
->locatePath('/search/' . $index . '/' . urlencode($term)));
}
public function iFillInCustomFieldsForThis($type) {
}
public function assertLoggedInWithPermissionsByName($name, $permissions) {
$permissions = array_map('trim', explode(',', $permissions));
$role = $this
->getDriver()
->roleCreate($permissions);
$manager = $this
->getUserManager();
$manager
->setCurrentUser($manager
->getUser($name));
$user = $manager
->getUser($name);
$this
->getDriver()
->userAddRole($user, $role);
$this->roles[] = $role;
$this
->login($user);
}
}