SocialDrupalContext.php in Open Social 8
File
tests/behat/features/bootstrap/SocialDrupalContext.php
View source
<?php
use Drupal\DrupalExtension\Context\DrupalContext;
use Behat\Mink\Element\Element;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Gherkin\Node\TableNode;
class SocialDrupalContext extends DrupalContext {
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 iWaitForTheQueueToBeEmpty() {
$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()) {
$queue_worker
->processItem($item->data);
$worker
->deleteItem($item);
}
}
}
}
}
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 iEnableTheTourSetting() {
\Drupal::configFactory()
->getEditable('social_tour.settings')
->set('social_tour_enabled', 1)
->save();
}
}