View source
<?php
namespace Drupal\realistic_dummy_content_api\Framework;
use Drupal\realistic_dummy_content_api\includes\RealisticDummyContentDevelGenerateGenerator;
class Framework implements FrameworkInterface {
private static $testFlag;
private static $instance;
private $implementor;
public static function instance() {
if (!self::$instance) {
self::$instance = new Framework();
}
return self::$instance;
}
public function implementor() {
if (!$this->implementor) {
$framework = realistic_dummy_content_api_version();
switch ($framework) {
case 'Drupal7':
module_load_include('php', 'realistic_dummy_content_api', 'src/Framework/Drupal7');
$this->implementor = new Drupal7();
break;
case 'Drupal8':
$this->implementor = new Drupal8();
break;
default:
throw new \Exception('No framework implementation class available for the framework ' . $framework);
}
}
return $this->implementor;
}
public function alter($type, &$data, &$context1 = NULL, &$context2 = NULL, &$context3 = NULL) {
return self::implementor()
->alter($type, $data, $context1, $context2, $context3);
}
public function configGet($name, $default = NULL) {
return $this
->implementor()
->configGet($name, $default);
}
public function createEntity($info = array()) {
$return = $this
->implementor()
->createEntity($info);
return $return;
}
public function debug($message, $info = NULL) {
return $this
->implementor()
->debug($message, $info);
}
public function develGenerate($info) {
return $this
->implementor()
->develGenerate($info);
}
public function entityIsDummy($entity, $type) {
$return = self::implementor()
->entityIsDummy($entity, $type);
return $return;
}
public function entityProperties($entity) {
return $this
->implementor()
->entityProperties($entity);
}
public function fieldInfoField($name) {
return $this
->implementor()
->fieldInfoField($name);
}
public function fieldInfoFields() {
return $this
->implementor()
->fieldInfoFields();
}
public function fieldTypeMachineName($info) {
return $this
->implementor()
->fieldTypeMachineName($info);
}
public function fileSave($drupal_file) {
$return = $this
->implementor()
->fileSave($drupal_file);
return $return;
}
public function filteredHtml() {
return $this
->implementor()
->filteredHtml();
}
public function formatProperty($type, $value, $options = array()) {
return $this
->implementor()
->formatProperty($type, $value, $options);
}
public function frameworkRoot() {
return $this
->implementor()
->frameworkRoot();
}
public function getAllVocabularies() {
return $this
->implementor()
->getAllVocabularies();
}
public function getBundleName($entity) {
return $this
->implementor()
->getBundleName($entity);
}
public function getEntityProperty(&$entity, $property) {
return $this
->implementor()
->getEntityProperty($entity, $property);
}
public function getPath($type, $name) {
return $this
->implementor()
->getPath($type, $name);
}
public function latestId($table = 'node', $key = 'nid') {
return db_query("SELECT {$key} FROM {$table} ORDER BY {$key} DESC LIMIT 1")
->fetchField();
}
public function moduleExists($module) {
return $this
->implementor()
->moduleExists($module);
}
public function moduleInvokeAll($hook) {
$args = func_get_args();
self::addTestFlag('moduleInvokeAll called');
$object = $this
->implementor();
return call_user_func_array(array(
&$object,
'moduleInvokeAll',
), $args);
}
public function moduleList() {
return $this
->implementor()
->moduleList();
}
public function hookEntityPresave($entity, $type) {
$return = self::implementor()
->hookEntityPresave($entity, $type);
static::addTestFlag('hookEntityPresave called');
return $return;
}
public function hookUserInsert(&$edit, $account, $category) {
return $this
->implementor()
->hookUserInsert($edit, $account, $category);
}
public function hookUserPresave(&$edit, $account, $category) {
return $this
->implementor()
->hookUserPresave($edit, $account, $category);
}
public function newVocabularyTerm($vocabulary, $name) {
return $this
->implementor()
->newVocabularyTerm($vocabulary, $name);
}
public function setEntityProperty(&$entity, $property, $value) {
return $this
->implementor()
->setEntityProperty($entity, $property, $value);
}
public function stateGet($name, $default = NULL) {
return $this
->implementor()
->stateGet($name, $default);
}
public function timerStart($id) {
return $this
->implementor()
->timerStart($id);
}
public function timerStop($id) {
return $this
->implementor()
->timerStop($id);
}
public function userPictureFilename($user) {
return $this
->implementor()
->userPictureFilename($user);
}
public function variableDel($variable) {
return $this
->implementor()
->variableDel($variable);
}
public function vocabularyIdentifier($vocabulary) {
return $this
->implementor()
->vocabularyIdentifier($vocabulary);
}
public function watchdog($message, $severity = 5) {
return $this
->implementor()
->watchdog($message, $severity);
}
public static function addTestFlag($flag) {
if (!is_array(self::$testFlag)) {
self::$testFlag = array();
}
self::$testFlag[$flag] = $flag;
}
public static function clearTestFlag($flag) {
unset(self::$testFlag[$flag]);
}
public function endToEndTests(&$errors, &$tests) {
$generator = new RealisticDummyContentDevelGenerateGenerator('user', 'user', 1, array(
'kill' => TRUE,
));
$generator
->generate();
$user = user_load(Framework::instance()
->latestId('users', 'uid'));
if (strpos(Framework::instance()
->userPictureFilename($user), 'dummyfile') !== FALSE) {
$tests[] = 'User picture substitution OK, and aliases work correctly.';
}
else {
$errors[] = 'User picture substitution does not work.';
}
$generator = new RealisticDummyContentDevelGenerateGenerator('node', 'article', 1, array(
'kill' => TRUE,
));
$generator
->generate();
}
public function frameworkSpecificTests(&$errors, &$tests) {
}
public static function getCallingFunction() {
$caller = debug_backtrace();
$caller = $caller[2];
return $caller['function'];
}
public static function getTestFlag($flag) {
return isset(self::$testFlag[$flag]);
}
public function selfTest() {
$all = get_class_methods(get_class($this));
$errors = array();
$tests = array();
foreach ($all as $method_name) {
if (substr($method_name, 0, strlen('test')) == 'test') {
continue;
}
$candidate = 'test' . ucfirst($method_name);
if (in_array($candidate, $all)) {
try {
$result = $this
->{$candidate}();
if ($result) {
$errors[] = $candidate . ' test failed.';
}
else {
$tests[] = $candidate . ' test passed.';
}
} catch (\Exception $e) {
$errors[] = $candidate . ' threw an exception: ' . $e
->getMessage();
}
}
}
$this
->frameworkSpecificTests($errors, $tests);
$this
->endToEndTests($errors, $tests);
self::debug('Errors:');
self::debug($errors);
self::debug('Passed tests:');
self::debug($tests);
return count($errors) != 0;
}
public function taxonomyLoadTree($vocabulary) {
return $this
->implementor()
->taxonomyLoadTree($vocabulary);
}
public function termId($term) {
return $this
->implementor()
->termId($term);
}
public function termName($term) {
return $this
->implementor()
->termName($term);
}
public static function testAddTestFlag() {
self::addTestFlag('whatever');
return !self::getTestFlag('whatever');
}
public function testClearTestFlag() {
self::addTestFlag('whatever');
self::clearTestFlag('whatever');
return self::getTestFlag('whatever');
}
public static function testCreateEntity() {
$entity = self::instance()
->createEntity();
return !is_object($entity);
}
public function testEntityIsDummy() {
return $this
->entityIsDummy('whatever', 'whatever') || !$this
->entityIsDummy((object) array(
'devel_generate' => TRUE,
), 'whatever');
}
public static function testGetEntityProperty() {
return self::testSetEntityProperty();
}
public static function testGetTestFlag() {
return self::testAddTestFlag();
}
public static function testHookEntityPresave() {
self::instance()
->createEntity();
return !static::getTestFlag('hookEntityPresave called');
}
public static function testInstance() {
return !is_a(self::instance(), '\\Drupal\\realistic_dummy_content_api\\Framework\\Framework');
}
public function testModuleInvokeAll() {
self::clearTestFlag('moduleInvokeAll called');
realistic_dummy_content_api_is_dummy($this
->createEntity(), 'node');
return !self::getTestFlag('moduleInvokeAll called');
}
public static function testSetEntityProperty() {
$entity = self::instance()
->createEntity();
self::instance()
->setEntityProperty($entity, 'title', 'whatever');
return self::instance()
->getEntityProperty($entity, 'title') != 'whatever';
}
public function vocabularyMachineName($vocabulary) {
return $this
->implementor()
->vocabularyMachineName($vocabulary);
}
}