EntityUpdateTestHelper.php in Entity Update 2.0.x
File
tests/modules/entity_update_tests/src/EntityUpdateTestHelper.php
View source
<?php
namespace Drupal\entity_update_tests;
use Drupal\Core\Database\Database;
class EntityUpdateTestHelper {
public static function getConfigName() {
return 'entity_update_tests.settings';
}
public static function getConfig($editable = FALSE) {
if ($editable) {
$config = \Drupal::configFactory()
->getEditable(static::getConfigName());
}
else {
$config = \Drupal::config(static::getConfigName());
}
return $config;
}
public static function fieldEnable($name, $enable = TRUE) {
$config = self::getConfig(TRUE);
$config
->set("fields." . $name, $enable);
$config
->save();
}
public static function fieldDisable($name) {
return self::fieldEnable($name, FALSE);
}
public static function fieldSetType($name, $type = 'string') {
$config = self::getConfig(TRUE);
$config
->set("fields." . $name, $type);
$config
->save();
}
public static function fieldStatus($name) {
return self::getConfig()
->get("fields." . $name);
}
public static function checkFieldList($table, array $fields) {
$fields_must = array_combine($fields, $fields);
$fields_curr = [];
$con = Database::getConnection();
if ($table_prefix = $con
->tablePrefix()) {
$table = $table_prefix . $table;
}
try {
$fields_list = $con
->query("DESCRIBE `{$table}`")
->fetchAll();
foreach ($fields_list as $field) {
$fields_curr[$field->Field] = $field->Field;
}
$array_diff = array_diff($fields_must, $fields_curr);
if (empty($array_diff)) {
$array_diff = array_diff($fields_curr, $fields_must);
if (empty($array_diff)) {
return TRUE;
}
}
return $array_diff;
} catch (\Exception $ex) {
return FALSE;
}
return FALSE;
}
}