View source
<?php
namespace Drupal\realistic_dummy_content_api\Framework;
class Drupal7 extends Framework implements FrameworkInterface {
public function hookEntityPresave($entity, $type) {
if ($type != 'user') {
$this
->genericEntityPresave($entity, $type);
}
}
public function fieldTypeMachineName($info) {
return $info['machine_name'];
}
public function entityProperties($entity) {
return (array) $entity;
}
public function userPictureFilename($user) {
return $user->picture->filename;
}
public function develGenerate($info) {
module_load_include('inc', 'devel_generate');
if ($info['entity_type'] == 'node') {
$info['users'] = array(
1,
);
$info['title_length'] = 3;
if ($info['kill']) {
devel_generate_content_kill($info);
}
for ($i = 0; $i < $info['num']; $i++) {
devel_generate_content_add_node($info);
}
}
elseif ($info['entity_type'] == 'user') {
devel_create_users($info['num'], $info['kill']);
}
}
public function hookUserInsert(&$edit, $account, $category) {
$filter = array(
'exclude' => array(
'picture',
),
);
$this
->genericEntityPresave($account, 'user', $filter);
}
public function frameworkSpecificTests(&$errors, &$tests) {
}
public function hookUserPresave(&$edit, $account, $category) {
if (isset($edit['picture_delete'])) {
return;
}
if (isset($account->uid) && $account->uid) {
$filter = array(
'include' => array(
'picture',
),
);
$user = (object) $edit;
$this
->genericEntityPresave($user, 'user', $filter);
$edit = (array) $user;
}
}
public function genericEntityPresave($entity, $type, $filter = array()) {
try {
if (realistic_dummy_content_api_is_dummy($entity, $type)) {
$candidate = $entity;
realistic_dummy_content_api_improve_dummy_content($candidate, $type, $filter);
realistic_dummy_content_api_validate($candidate, $type);
}
} catch (\Exception $e) {
drupal_set_message(t('realistic_dummy_content_api failed to modify dummy objects: message: @m', array(
'@m' => $e
->getMessage(),
)), 'error', FALSE);
}
}
public function alter($type, &$data, &$context1 = NULL, &$context2 = NULL, &$context3 = NULL) {
return drupal_alter($type, $data, $context1, $context2, $context3);
}
public function fieldInfoFields() {
return field_info_fields();
}
public function fieldInfoField($field) {
return field_info_field($field);
}
public function moduleList() {
return module_list();
}
public function moduleInvokeAll($hook) {
$args = func_get_args();
return call_user_func_array('module_invoke_all', $args);
}
public function entityIsDummy($entity, $type) {
$return = FALSE;
if (isset($entity->devel_generate)) {
return TRUE;
}
switch ($type) {
case 'user':
$suffix = '.invalid';
if (isset($entity->mail) && $this
->drupalSubstr($entity->mail, strlen($entity->mail) - strlen($suffix)) == $suffix) {
return TRUE;
}
break;
default:
break;
}
return $return;
}
public function drupalSubstr($text, $start) {
return drupal_substr($text, $start);
}
public function getBundleName($entity) {
return $entity->type;
}
public function configGet($name, $default = NULL) {
return variable_get($name, $default);
}
public function stateGet($name, $default = NULL) {
return variable_get($name, $default);
}
public function setEntityProperty(&$entity, $property, $value) {
$entity->{$property} = $value;
}
public function formatProperty($type, $value, $options = array()) {
switch ($type) {
case 'file':
return array(
LANGUAGE_NONE => array(
(array) $value,
),
);
case 'value':
case 'tid':
return array(
LANGUAGE_NONE => array(
array_merge($options, array(
$type => $value,
)),
),
);
case 'text_with_summary':
return array(
LANGUAGE_NONE => array(
array_merge($options, array(
'value' => $value,
)),
),
);
default:
throw new \Exception('Unknown property type ' . $type);
}
}
public function getEntityProperty(&$entity, $property) {
return $entity->{$property};
}
public function createEntity($info = array()) {
if (isset($info['entity_type'])) {
$entity_type = $info['entity_type'];
}
else {
$entity_type = 'node';
}
$entity = new \stdClass();
switch ($entity_type) {
case 'node':
$entity->title = rand(100000, 999999);
$entity->type = $this
->getDefaultNodeType();
node_save($entity);
break;
case 'user':
$options = array(
'name' => rand(1000000, 9999999),
);
if (isset($info['dummy']) && $info['dummy']) {
$options['mail'] = $options['name'] . '@example.invalid';
}
$entity = user_save(drupal_anonymous_user(), $options);
break;
default:
throw new \Exception('Unknown entity type ' . $entity_type);
}
return $entity;
}
public function getDefaultNodeType() {
return 'article';
}
public function testsGetDefaultNodeType() {
return !is_string($this
->getDefaultNodeType());
}
public function debug($message, $info = NULL) {
if ($this
->moduleExists('devel')) {
dpm($message, $info);
}
$this
->watchdog('<pre>' . print_r(array(
$info => $message,
), TRUE) . '</pre>');
}
public function getPath($type, $name) {
return drupal_get_path($type, $name);
}
public function frameworkRoot() {
return DRUPAL_ROOT;
}
public function moduleExists($module) {
return module_exists($module);
}
public function watchdog($message, $severity = 5) {
watchdog('realistic_dummy_content_api', $message, $severity);
}
public function getAllVocabularies() {
$return = taxonomy_get_vocabularies();
return $return;
}
public function fileSave($drupal_file) {
$return = file_save($drupal_file);
return $return;
}
public function testHookUserInsert() {
self::clearTestFlag('hookUserInsert called');
$this
->createEntity(array(
'entity_type' => 'user',
));
return !static::getTestFlag('hookUserInsert called');
}
public function testHookUserPresave() {
self::clearTestFlag('hookUserPresave called');
$this
->createEntity(array(
'entity_type' => 'user',
));
return !static::getTestFlag('hookUserPresave called');
}
public function vocabularyIdentifier($vocabulary) {
return $vocabulary->vid;
}
public function newVocabularyTerm($vocabulary, $name) {
$term = new \stdClass();
$term->name = $name;
$term->vid = $vocabulary->vid;
taxonomy_term_save($term);
return $term;
}
public function timerStart($id) {
return timer_start($id);
}
public function timerStop($id) {
return timer_stop($id);
}
public function filteredHtml() {
return 'filtered_html';
}
public function taxonomyLoadTree($vocabulary) {
return taxonomy_get_tree($this
->vocabularyIdentifier($vocabulary));
}
public function termId($term) {
if (isset($term->tid) && $term->tid) {
return $term->tid;
}
else {
throw new \Exception('tid could not be determined');
}
}
public function termName($term) {
if (isset($term->name) && $term->name) {
return $term->name;
}
else {
throw new \Exception('term name could not be determined');
}
}
public function variableDel($variable) {
variable_del($variable);
}
public function vocabularyMachineName($vocabulary) {
if (isset($vocabulary->machine_name) && $vocabulary->machine_name) {
return $vocabulary->machine_name;
}
else {
throw new \Exception('vocabulary machine name could not be determined');
}
}
}