// Iterate over aliases in the queue.
foreach ($rs as $record) {
- $info = restore_dbops::decode_backup_temp_info($record->info);
+ $info = backup_controller_dbops::decode_backup_temp_info($record->info);
// Try to pick a repository instance that should serve the alias.
$repository = $this->choose_repository($info);
* In advanced cases an array may be useful such as in situations requiring the multi-key functionality.
* @param int $strictness One of IGNORE_MISSING | MUST_EXIST
* @return mixed|false The data from the cache or false if the key did not exist within the cache.
- * @throws moodle_exception
+ * @throws coding_exception
*/
public function get($key, $strictness = IGNORE_MISSING) {
// 1. Parse the key.
}
// 5. Validate strictness.
if ($strictness === MUST_EXIST && $result === false) {
- throw new moodle_exception('Requested key did not exist in any cache stores and could not be loaded.');
+ throw new coding_exception('Requested key did not exist in any cache stores and could not be loaded.');
}
// 6. Set it to the store if we got it from the loader/datasource.
if ($setaftervalidation) {
* @return array An array of key value pairs for the items that could be retrieved from the cache.
* If MUST_EXIST was used and not all keys existed within the cache then an exception will be thrown.
* Otherwise any key that did not exist will have a data value of false within the results.
- * @throws moodle_exception
+ * @throws coding_exception
*/
public function get_many(array $keys, $strictness = IGNORE_MISSING) {
$missingkeys = array();
foreach ($result as $key => $value) {
if ($value === false) {
- $missingkeys[] = ($usingloader) ? $key : $parsedkeys[$key];
+ $missingkeys[] = $parsedkeys[$key];
}
}
if (!empty($missingkeys)) {
$resultmissing = $this->datasource->load_many_for_cache($missingkeys);
}
foreach ($resultmissing as $key => $value) {
- $pkey = ($usingloader) ? $key : $keysparsed[$key];
- $realkey = ($usingloader) ? $parsedkeys[$key] : $key;
- $result[$pkey] = $value;
+ $result[$keysparsed[$key]] = $value;
if ($value !== false) {
- $this->set($realkey, $value);
+ $this->set($key, $value);
}
}
unset($resultmissing);
if ($strictness === MUST_EXIST) {
foreach ($keys as $key) {
if (!array_key_exists($key, $fullresult)) {
- throw new moodle_exception('Not all the requested keys existed within the cache stores.');
+ throw new coding_exception('Not all the requested keys existed within the cache stores.');
}
}
}
if ($this->perfdebug) {
cache_helper::record_cache_set($this->storetype, $this->definition->get_id());
}
+ if ($this->loader !== false) {
+ // We have a loader available set it there as well.
+ // We have to let the loader do its own parsing of data as it may be unique.
+ $this->loader->set($key, $data);
+ }
if (is_object($data) && $data instanceof cacheable_object) {
$data = new cache_cached_object($data);
} else if (!is_scalar($data)) {
* Removes references where required.
*
* @param stdClass|array $data
+ * @return mixed What ever was put in but without any references.
*/
protected function unref($data) {
if ($this->definition->uses_simple_data()) {
* ... if they care that is.
*/
public function set_many(array $keyvaluearray) {
+ if ($this->loader !== false) {
+ // We have a loader available set it there as well.
+ // We have to let the loader do its own parsing of data as it may be unique.
+ $this->loader->set_many($keyvaluearray);
+ }
$data = array();
$simulatettl = $this->has_a_ttl() && !$this->store_supports_native_ttl();
$usepersistcache = $this->is_using_persist_cache();
* Returns the loader associated with this instance.
*
* @since 2.4.4
- * @return cache_loader|false
+ * @return cache|false
*/
protected function get_loader() {
return $this->loader;
* @param string|int $key The key for the data being requested.
* @param int $strictness One of IGNORE_MISSING | MUST_EXIST
* @return mixed|false The data from the cache or false if the key did not exist within the cache.
- * @throws moodle_exception
*/
public function get($key, $strictness = IGNORE_MISSING) {
if ($this->requirelockingread && $this->check_lock_state($key) === false) {
* @return array An array of key value pairs for the items that could be retrieved from the cache.
* If MUST_EXIST was used and not all keys existed within the cache then an exception will be thrown.
* Otherwise any key that did not exist will have a data value of false within the results.
- * @throws moodle_exception
+ * @throws coding_exception
*/
public function get_many(array $keys, $strictness = IGNORE_MISSING) {
if ($this->requirelockingread) {
* @todo we should support locking in the session as well. Should be pretty simple to set up.
*
* @internal don't use me directly.
+ * @method cache_store|cache_is_searchable get_store() Returns the cache store which must implement both cache_is_searchable.
*
* @package core
* @category cache
*/
const KEY_PREFIX = 'sess_';
+ /**
+ * This is the key used to track last access.
+ */
+ const LASTACCESS = '__lastaccess__';
+
/**
* Override the cache::construct method.
*
* @param cache_definition $definition
* @param cache_store $store
* @param cache_loader|cache_data_source $loader
- * @return void
*/
public function __construct(cache_definition $definition, cache_store $store, $loader = null) {
// First up copy the loadeduserid to the current user id.
$this->currentuserid = self::$loadeduserid;
parent::__construct($definition, $store, $loader);
+
+ // This will trigger check tracked user. If this gets removed a call to that will need to be added here in its place.
+ $this->set(self::LASTACCESS, cache::now());
+
if ($definition->has_invalidation_events()) {
$lastinvalidation = $this->get('lastsessioninvalidation');
if ($lastinvalidation === false) {
}
}
+ /**
+ * Sets the session id for the loader.
+ */
+ protected function set_session_id() {
+ $this->sessionid = preg_replace('#[^a-zA-Z0-9_]#', '_', session_id());
+ }
+
+ /**
+ * Returns the prefix used for all keys.
+ * @return string
+ */
+ protected function get_key_prefix() {
+ return 'u'.$this->currentuserid.'_'.$this->sessionid;
+ }
+
/**
* Parses the key turning it into a string (or array is required) suitable to be passed to the cache store.
*
* @return string|array String unless the store supports multi-identifiers in which case an array if returned.
*/
protected function parse_key($key) {
- if ($key === 'lastaccess') {
- $key = '__lastaccess__';
+ $prefix = $this->get_key_prefix();
+ if ($key === self::LASTACCESS) {
+ return $key.$prefix;
}
- return 'sess_'.parent::parse_key($key);
+ return $prefix.'_'.parent::parse_key($key);
}
/**
* Check that this cache instance is tracking the current user.
*/
protected function check_tracked_user() {
- if (isset($_SESSION['USER']->id)) {
+ if (isset($_SESSION['USER']->id) && $_SESSION['USER']->id !== null) {
// Get the id of the current user.
$new = $_SESSION['USER']->id;
} else {
// This way we don't bloat the session.
$this->purge();
// Update the session id just in case!
- $this->sessionid = session_id();
+ $this->set_session_id();
}
self::$loadeduserid = $new;
$this->currentuserid = $new;
} else if ($new !== $this->currentuserid) {
// The current user matches the loaded user but not the user last used by this cache.
- $this->purge();
+ $this->purge_current_user();
$this->currentuserid = $new;
// Update the session id just in case!
- $this->sessionid = session_id();
+ $this->set_session_id();
}
}
/**
- * Gets the session data.
- *
- * @param bool $force If true the session data will be loaded from the store again.
- * @return array An array of session data.
- */
- protected function get_session_data($force = false) {
- if ($this->sessionid === null) {
- $this->sessionid = session_id();
- }
- if (is_array($this->session) && !$force) {
- return $this->session;
- }
- $session = parent::get($this->sessionid);
- if ($session === false) {
- $session = array();
- }
- // We have to write here to ensure that the lastaccess time is recorded.
- // And also in order to ensure the session entry exists as when we save it on __destruct
- // $CFG is likely to have already been destroyed.
- $this->save_session($session);
- return $this->session;
- }
-
- /**
- * Saves the session data.
- *
- * This function also updates the last access time.
- *
- * @param array $session
- * @return bool
+ * Purges the session cache of all data belonging to the current user.
*/
- protected function save_session(array $session) {
- $session['lastaccess'] = time();
- $this->session = $session;
- return parent::set($this->sessionid, $this->session);
+ public function purge_current_user() {
+ $keys = $this->get_store()->find_all($this->get_key_prefix());
+ $this->get_store()->delete_many($keys);
}
/**
* In advanced cases an array may be useful such as in situations requiring the multi-key functionality.
* @param int $strictness One of IGNORE_MISSING | MUST_EXIST
* @return mixed|false The data from the cache or false if the key did not exist within the cache.
- * @throws moodle_exception
+ * @throws coding_exception
*/
public function get($key, $strictness = IGNORE_MISSING) {
// Check the tracked user.
// 2. Parse the key.
$parsedkey = $this->parse_key($key);
// 3. Get it from the store.
- $result = false;
- $session = $this->get_session_data();
- if (array_key_exists($parsedkey, $session)) {
- $result = $session[$parsedkey];
+ $result = $this->get_store()->get($parsedkey);
+ if ($result !== false) {
if ($result instanceof cache_ttl_wrapper) {
if ($result->has_expired()) {
$this->get_store()->delete($parsedkey);
}
}
// 4. Load if from the loader/datasource if we don't already have it.
- $setaftervalidation = false;
if ($result === false) {
if ($this->perfdebug) {
- cache_helper::record_cache_miss('**static session**', $this->get_definition()->get_id());
+ cache_helper::record_cache_miss($this->storetype, $this->get_definition()->get_id());
}
if ($this->get_loader() !== false) {
// We must pass the original (unparsed) key to the next loader in the chain.
} else if ($this->get_datasource() !== false) {
$result = $this->get_datasource()->load_for_cache($key);
}
- $setaftervalidation = ($result !== false);
+ // 5. Set it to the store if we got it from the loader/datasource.
+ if ($result !== false) {
+ $this->set($key, $result);
+ }
} else if ($this->perfdebug) {
- cache_helper::record_cache_hit('**static session**', $this->get_definition()->get_id());
+ cache_helper::record_cache_hit($this->storetype, $this->get_definition()->get_id());
}
// 5. Validate strictness.
if ($strictness === MUST_EXIST && $result === false) {
- throw new moodle_exception('Requested key did not exist in any cache stores and could not be loaded.');
- }
- // 6. Set it to the store if we got it from the loader/datasource.
- if ($setaftervalidation) {
- $this->set($key, $result);
+ throw new coding_exception('Requested key did not exist in any cache stores and could not be loaded.');
}
- // 7. Make sure we don't pass back anything that could be a reference.
+ // 6. Make sure we don't pass back anything that could be a reference.
// We don't want people modifying the data in the cache.
if (!is_scalar($result)) {
// If data is an object it will be a reference.
*/
public function set($key, $data) {
$this->check_tracked_user();
+ $loader = $this->get_loader();
+ if ($loader !== false) {
+ // We have a loader available set it there as well.
+ // We have to let the loader do its own parsing of data as it may be unique.
+ $loader->set($key, $data);
+ }
if ($this->perfdebug) {
- cache_helper::record_cache_set('**static session**', $this->get_definition()->get_id());
+ cache_helper::record_cache_set($this->storetype, $this->get_definition()->get_id());
}
if (is_object($data) && $data instanceof cacheable_object) {
$data = new cache_cached_object($data);
$data = $this->unref($data);
}
// We dont' support native TTL here as we consolidate data for sessions.
- if ($this->has_a_ttl()) {
+ if ($this->has_a_ttl() && !$this->store_supports_native_ttl()) {
$data = new cache_ttl_wrapper($data, $this->get_definition()->get_ttl());
}
- $session = $this->get_session_data();
- $session[$this->parse_key($key)] = $data;
- return $this->save_session($session);
+ return $this->get_store()->set($this->parse_key($key), $data);
}
/**
* @return bool True of success, false otherwise.
*/
public function delete($key, $recurse = true) {
- $this->check_tracked_user();
$parsedkey = $this->parse_key($key);
if ($recurse && $this->get_loader() !== false) {
// Delete from the bottom of the stack first.
$this->get_loader()->delete($key, $recurse);
}
- $session = $this->get_session_data();
- unset($session[$parsedkey]);
- return $this->save_session($session);
+ return $this->get_store()->delete($parsedkey);
}
/**
* @return array An array of key value pairs for the items that could be retrieved from the cache.
* If MUST_EXIST was used and not all keys existed within the cache then an exception will be thrown.
* Otherwise any key that did not exist will have a data value of false within the results.
- * @throws moodle_exception
+ * @throws coding_exception
*/
public function get_many(array $keys, $strictness = IGNORE_MISSING) {
$this->check_tracked_user();
- $return = array();
+ $parsedkeys = array();
+ $keymap = array();
foreach ($keys as $key) {
- $return[$key] = $this->get($key, $strictness);
+ $parsedkey = $this->parse_key($key);
+ $parsedkeys[$key] = $parsedkey;
+ $keymap[$parsedkey] = $key;
+ }
+ $result = $this->get_store()->get_many($parsedkeys);
+ $return = array();
+ $missingkeys = array();
+ $hasmissingkeys = false;
+ foreach ($result as $parsedkey => $value) {
+ $key = $keymap[$parsedkey];
+ if ($value instanceof cache_ttl_wrapper) {
+ /* @var cache_ttl_wrapper $value */
+ if ($value->has_expired()) {
+ $this->delete($keymap[$parsedkey]);
+ $value = false;
+ } else {
+ $value = $value->data;
+ }
+ }
+ if ($value instanceof cache_cached_object) {
+ /* @var cache_cached_object $value */
+ $value = $value->restore_object();
+ }
+ $return[$key] = $value;
+ if ($value === false) {
+ $hasmissingkeys = true;
+ $missingkeys[$parsedkey] = $key;
+ }
+ }
+ if ($hasmissingkeys) {
+ // We've got missing keys - we've got to check any loaders or data sources.
+ $loader = $this->get_loader();
+ $datasource = $this->get_datasource();
+ if ($loader !== false) {
+ foreach ($loader->get_many($missingkeys) as $key => $value) {
+ if ($value !== false) {
+ $return[$key] = $value;
+ unset($missingkeys[$parsedkeys[$key]]);
+ }
+ }
+ }
+ $hasmissingkeys = count($missingkeys) > 0;
+ if ($datasource !== false && $hasmissingkeys) {
+ // We're still missing keys but we've got a datasource.
+ foreach ($datasource->load_many_for_cache($missingkeys) as $key => $value) {
+ if ($value !== false) {
+ $return[$key] = $value;
+ unset($missingkeys[$parsedkeys[$key]]);
+ }
+ }
+ $hasmissingkeys = count($missingkeys) > 0;
+ }
}
+ if ($hasmissingkeys && $strictness === MUST_EXIST) {
+ throw new coding_exception('Requested key did not exist in any cache stores and could not be loaded.');
+ }
+
return $return;
+
}
/**
* @return int The number of items successfully deleted.
*/
public function delete_many(array $keys, $recurse = true) {
- $this->check_tracked_user();
$parsedkeys = array_map(array($this, 'parse_key'), $keys);
if ($recurse && $this->get_loader() !== false) {
// Delete from the bottom of the stack first.
$this->get_loader()->delete_many($keys, $recurse);
}
- $session = $this->get_session_data();
- foreach ($parsedkeys as $parsedkey) {
- unset($session[$parsedkey]);
- }
- $this->save_session($session);
- return count($keys);
+ return $this->get_store()->delete_many($parsedkeys);
}
/**
*/
public function set_many(array $keyvaluearray) {
$this->check_tracked_user();
- $session = $this->get_session_data();
- $simulatettl = $this->has_a_ttl();
+ $loader = $this->get_loader();
+ if ($loader !== false) {
+ // We have a loader available set it there as well.
+ // We have to let the loader do its own parsing of data as it may be unique.
+ $loader->set_many($keyvaluearray);
+ }
+ $data = array();
+ $definitionid = $this->get_definition()->get_ttl();
+ $simulatettl = $this->has_a_ttl() && !$this->store_supports_native_ttl();
foreach ($keyvaluearray as $key => $value) {
if (is_object($value) && $value instanceof cacheable_object) {
$value = new cache_cached_object($value);
$value = $this->unref($value);
}
if ($simulatettl) {
- $value = new cache_ttl_wrapper($value, $this->get_definition()->get_ttl());
+ $value = new cache_ttl_wrapper($value, $definitionid);
}
- $parsedkey = $this->parse_key($key);
- $session[$parsedkey] = $value;
+ $data[$key] = array(
+ 'key' => $this->parse_key($key),
+ 'value' => $value
+ );
}
if ($this->perfdebug) {
- cache_helper::record_cache_set($this->storetype, $this->get_definition()->get_id());
+ cache_helper::record_cache_set($this->storetype, $definitionid);
}
- $this->save_session($session);
- return count($keyvaluearray);
+ return $this->get_store()->set_many($data);
}
/**
* @return bool True on success, false otherwise
*/
public function purge() {
- // 1. Purge the session object.
- $this->session = array();
- // 2. Delete the record for this users session from the store.
- $this->get_store()->delete($this->sessionid);
- // 3. Optionally purge any stacked loaders in the same way.
+ $this->get_store()->purge();
if ($this->get_loader()) {
- $this->get_loader()->delete($this->sessionid);
+ $this->get_loader()->purge();
}
return true;
}
public function has($key, $tryloadifpossible = false) {
$this->check_tracked_user();
$parsedkey = $this->parse_key($key);
- $session = $this->get_session_data();
- $has = false;
- if ($this->has_a_ttl()) {
+ $store = $this->get_store();
+ if ($this->has_a_ttl() && !$this->store_supports_native_ttl()) {
// The data has a TTL and the store doesn't support it natively.
// We must fetch the data and expect a ttl wrapper.
- if (array_key_exists($parsedkey, $session)) {
- $data = $session[$parsedkey];
- $has = ($data instanceof cache_ttl_wrapper && !$data->has_expired());
- }
+ $data = $store->get($parsedkey);
+ $has = ($data instanceof cache_ttl_wrapper && !$data->has_expired());
+ } else if (!$this->store_supports_key_awareness()) {
+ // The store doesn't support key awareness, get the data and check it manually... puke.
+ // Either no TTL is set of the store supports its handling natively.
+ $data = $store->get($parsedkey);
+ $has = ($data !== false);
} else {
- $has = array_key_exists($parsedkey, $session);
+ // The store supports key awareness, this is easy!
+ // Either no TTL is set of the store supports its handling natively.
+ /* @var cache_store|cache_is_key_aware $store */
+ $has = $store->has($parsedkey);
}
if (!$has && $tryloadifpossible) {
+ $result = null;
if ($this->get_loader() !== false) {
- $result = $this->get_loader()->get($key);
+ $result = $this->get_loader()->get($parsedkey);
} else if ($this->get_datasource() !== null) {
$result = $this->get_datasource()->load_for_cache($key);
}
*/
public function has_all(array $keys) {
$this->check_tracked_user();
- $session = $this->get_session_data();
- foreach ($keys as $key) {
- $has = false;
- $parsedkey = $this->parse_key($key);
- if ($this->has_a_ttl()) {
- // The data has a TTL and the store doesn't support it natively.
- // We must fetch the data and expect a ttl wrapper.
- if (array_key_exists($parsedkey, $session)) {
- $data = $session[$parsedkey];
- $has = ($data instanceof cache_ttl_wrapper && !$data->has_expired());
+ if (($this->has_a_ttl() && !$this->store_supports_native_ttl()) || !$this->store_supports_key_awareness()) {
+ foreach ($keys as $key) {
+ if (!$this->has($key)) {
+ return false;
}
- } else {
- $has = array_key_exists($parsedkey, $session);
- }
- if (!$has) {
- return false;
}
+ return true;
}
- return true;
+ // The cache must be key aware and if support native ttl if it a ttl is set.
+ /* @var cache_store|cache_is_key_aware $store */
+ $store = $this->get_store();
+ return $store->has_all(array_map(array($this, 'parse_key'), $keys));
}
/**
* @return bool True if the cache has at least one of the given keys
*/
public function has_any(array $keys) {
- $this->check_tracked_user();
- $session = $this->get_session_data();
- foreach ($keys as $key) {
- $has = false;
- $parsedkey = $this->parse_key($key);
- if ($this->has_a_ttl()) {
- // The data has a TTL and the store doesn't support it natively.
- // We must fetch the data and expect a ttl wrapper.
- if (array_key_exists($parsedkey, $session)) {
- $data = $session[$parsedkey];
- $has = ($data instanceof cache_ttl_wrapper && !$data->has_expired());
+ if (($this->has_a_ttl() && !$this->store_supports_native_ttl()) || !$this->store_supports_key_awareness()) {
+ foreach ($keys as $key) {
+ if ($this->has($key)) {
+ return true;
}
- } else {
- $has = array_key_exists($parsedkey, $session);
- }
- if ($has) {
- return true;
}
+ return false;
}
- return false;
+ /* @var cache_store|cache_is_key_aware $store */
+ $store = $this->get_store();
+ return $store->has_any(array_map(array($this, 'parse_key'), $keys));
}
/**
/**
* The maximum size for the store, or false if there isn't one.
- * @var bool
+ * @var bool|int
*/
protected $maxsize = false;
*/
public function initialise(cache_definition $definition) {
$this->storeid = $definition->generate_definition_hash();
- $this->store = &self::register_store_id($definition->get_id());
+ $this->store = &self::register_store_id($this->name.'-'.$definition->get_id());
$this->ttl = $definition->get_ttl();
$maxsize = $definition->get_maxsize();
if ($maxsize !== null) {
$this->maxsize = abs((int)$maxsize);
$this->storecount = count($this->store);
}
+ $this->check_ttl();
}
/**
public function get($key) {
if (isset($this->store[$key])) {
if ($this->ttl == 0) {
- return $this->store[$key][0];
+ $value = $this->store[$key][0];
+ if ($this->maxsize !== false) {
+ // Make sure the element is now in the end of array.
+ $this->set($key, $value);
+ }
+ return $value;
} else if ($this->store[$key][1] >= (cache::now() - $this->ttl)) {
return $this->store[$key][0];
+ } else {
+ // Element is present but has expired.
+ $this->check_ttl();
}
}
return false;
*/
public function get_many($keys) {
$return = array();
+ $maxtime = 0;
if ($this->ttl != 0) {
$maxtime = cache::now() - $this->ttl;
}
+ $hasexpiredelements = false;
foreach ($keys as $key) {
$return[$key] = false;
if (isset($this->store[$key])) {
if ($this->ttl == 0) {
$return[$key] = $this->store[$key][0];
+ if ($this->maxsize !== false) {
+ // Make sure the element is now in the end of array.
+ $this->set($key, $return[$key], false);
+ }
} else if ($this->store[$key][1] >= $maxtime) {
$return[$key] = $this->store[$key][0];
+ } else {
+ $hasexpiredelements = true;
}
}
}
+ if ($hasexpiredelements) {
+ // There are some elements that are present but have expired.
+ $this->check_ttl();
+ }
return $return;
}
*
* @param string $key The key to use.
* @param mixed $data The data to set.
- * @param bool $testmaxsize If set to true then we test the maxsize arg and reduce if required.
+ * @param bool $testmaxsize If set to true then we test the maxsize arg and reduce if required. If this is set to false you will
+ * need to perform these checks yourself. This allows for bulk set's to be performed and maxsize tests performed once.
* @return bool True if the operation was a success false otherwise.
*/
public function set($key, $data, $testmaxsize = true) {
$testmaxsize = ($testmaxsize && $this->maxsize !== false);
- if ($testmaxsize) {
- $increment = (!isset($this->store[$key]));
+ $increment = $this->maxsize !== false && !isset($this->store[$key]);
+ if (($this->maxsize !== false && !$increment) || $this->ttl != 0) {
+ // Make sure the element is added to the end of $this->store array.
+ unset($this->store[$key]);
}
- if ($this->ttl == 0) {
- $this->store[$key][0] = $data;
+ if ($this->ttl === 0) {
+ $this->store[$key] = array($data, 0);
} else {
$this->store[$key] = array($data, cache::now());
}
- if ($testmaxsize && $increment) {
+ if ($increment) {
$this->storecount++;
- if ($this->storecount > $this->maxsize) {
- $this->reduce_for_maxsize();
- }
+ }
+ if ($testmaxsize && $this->storecount > $this->maxsize) {
+ $this->reduce_for_maxsize();
}
return true;
}
*/
public function set_many(array $keyvaluearray) {
$count = 0;
+ $increment = 0;
foreach ($keyvaluearray as $pair) {
- $this->set($pair['key'], $pair['value'], false);
+ $key = $pair['key'];
+ $data = $pair['value'];
$count++;
+ if ($this->maxsize !== false || $this->ttl !== 0) {
+ // Make sure the element is added to the end of $this->store array.
+ $this->delete($key);
+ $increment++;
+ } else if (!isset($this->store[$key])) {
+ $increment++;
+ }
+ if ($this->ttl === 0) {
+ $this->store[$key] = array($data, 0);
+ } else {
+ $this->store[$key] = array($data, cache::now());
+ }
}
if ($this->maxsize !== false) {
- $this->storecount += $count;
+ $this->storecount += $increment;
if ($this->storecount > $this->maxsize) {
$this->reduce_for_maxsize();
}
* @return bool
*/
public function has_all(array $keys) {
+ $maxtime = 0;
if ($this->ttl != 0) {
$maxtime = cache::now() - $this->ttl;
}
* @return bool
*/
public function has_any(array $keys) {
+ $maxtime = 0;
if ($this->ttl != 0) {
$maxtime = cache::now() - $this->ttl;
}
* @return bool Returns true if the operation was a success, false otherwise.
*/
public function delete($key) {
- $result = isset($this->store[$key]);
+ if (!isset($this->store[$key])) {
+ return false;
+ }
unset($this->store[$key]);
if ($this->maxsize !== false) {
$this->storecount--;
}
- return $result;
+ return true;
}
/**
* @return int The number of items successfully deleted.
*/
public function delete_many(array $keys) {
- $count = 0;
+ // The number of items that have actually being removed.
+ $reduction = 0;
foreach ($keys as $key) {
if (isset($this->store[$key])) {
- $count++;
+ $reduction++;
}
unset($this->store[$key]);
}
if ($this->maxsize !== false) {
- $this->storecount -= $count;
+ $this->storecount -= $reduction;
}
- return $count;
+ return $reduction;
}
/**
return $this->name;
}
+ /**
+ * Removes expired elements.
+ * @return int number of removed elements
+ */
+ protected function check_ttl() {
+ if ($this->ttl === 0) {
+ return 0;
+ }
+ $maxtime = cache::now() - $this->ttl;
+ $count = 0;
+ for ($value = reset($this->store); $value !== false; $value = next($this->store)) {
+ if ($value[1] >= $maxtime) {
+ // We know that elements are sorted by ttl so no need to continue.
+ break;
+ }
+ $count++;
+ }
+ if ($count) {
+ // Remove first $count elements as they are expired.
+ $this->store = array_slice($this->store, $count, null, true);
+ if ($this->maxsize !== false) {
+ $this->storecount -= $count;
+ }
+ }
+ return $count;
+ }
+
/**
* Finds all of the keys being stored in the cache store instance.
*
* @return array
*/
public function find_all() {
+ $this->check_ttl();
return array_keys($this->store);
}
* Finds all of the keys whose keys start with the given prefix.
*
* @param string $prefix
+ * @return array An array of keys.
*/
public function find_by_prefix($prefix) {
$return = array();
}
return $return;
}
+
+ /**
+ * This store supports native TTL handling.
+ * @return bool
+ */
+ public function store_supports_native_ttl() {
+ return true;
+ }
}
* Test the maxsize option.
*/
public function test_maxsize() {
- $defid = 'phpunit/testmaxsize';
$config = cache_config_phpunittest::instance();
- $config->phpunit_add_definition($defid, array(
+ $config->phpunit_add_definition('phpunit/one', array(
'mode' => cache_store::MODE_SESSION,
'component' => 'phpunit',
- 'area' => 'testmaxsize',
+ 'area' => 'one',
'maxsize' => 3
));
- $definition = cache_definition::load($defid, $config->get_definition_by_id($defid));
- $instance = cachestore_session::initialise_test_instance($definition);
- $this->assertTrue($instance->set('key1', 'value1'));
- $this->assertTrue($instance->set('key2', 'value2'));
- $this->assertTrue($instance->set('key3', 'value3'));
+ $config->phpunit_add_definition('phpunit/two', array(
+ 'mode' => cache_store::MODE_SESSION,
+ 'component' => 'phpunit',
+ 'area' => 'two',
+ 'maxsize' => 3
+ ));
+
+ $cacheone = cache::make('phpunit', 'one');
- $this->assertTrue($instance->has('key1'));
- $this->assertTrue($instance->has('key2'));
- $this->assertTrue($instance->has('key3'));
+ $this->assertTrue($cacheone->set('key1', 'value1'));
+ $this->assertTrue($cacheone->set('key2', 'value2'));
+ $this->assertTrue($cacheone->set('key3', 'value3'));
- $this->assertTrue($instance->set('key4', 'value4'));
- $this->assertTrue($instance->set('key5', 'value5'));
+ $this->assertTrue($cacheone->has('key1'));
+ $this->assertTrue($cacheone->has('key2'));
+ $this->assertTrue($cacheone->has('key3'));
- $this->assertFalse($instance->has('key1'));
- $this->assertFalse($instance->has('key2'));
- $this->assertTrue($instance->has('key3'));
- $this->assertTrue($instance->has('key4'));
- $this->assertTrue($instance->has('key5'));
+ $this->assertTrue($cacheone->set('key4', 'value4'));
+ $this->assertTrue($cacheone->set('key5', 'value5'));
- $this->assertFalse($instance->get('key1'));
- $this->assertFalse($instance->get('key2'));
- $this->assertEquals('value3', $instance->get('key3'));
- $this->assertEquals('value4', $instance->get('key4'));
- $this->assertEquals('value5', $instance->get('key5'));
+ $this->assertFalse($cacheone->has('key1'));
+ $this->assertFalse($cacheone->has('key2'));
+ $this->assertTrue($cacheone->has('key3'));
+ $this->assertTrue($cacheone->has('key4'));
+ $this->assertTrue($cacheone->has('key5'));
+
+ $this->assertFalse($cacheone->get('key1'));
+ $this->assertFalse($cacheone->get('key2'));
+ $this->assertEquals('value3', $cacheone->get('key3'));
+ $this->assertEquals('value4', $cacheone->get('key4'));
+ $this->assertEquals('value5', $cacheone->get('key5'));
// Test adding one more.
- $this->assertTrue($instance->set('key6', 'value6'));
- $this->assertFalse($instance->get('key3'));
+ $this->assertTrue($cacheone->set('key6', 'value6'));
+ $this->assertFalse($cacheone->get('key3'));
// Test reducing and then adding to make sure we don't lost one.
- $this->assertTrue($instance->delete('key6'));
- $this->assertTrue($instance->set('key7', 'value7'));
- $this->assertEquals('value4', $instance->get('key4'));
+ $this->assertTrue($cacheone->delete('key6'));
+ $this->assertTrue($cacheone->set('key7', 'value7'));
+ $this->assertEquals('value4', $cacheone->get('key4'));
// Set the same key three times to make sure it doesn't count overrides.
for ($i = 0; $i < 3; $i++) {
- $this->assertTrue($instance->set('key8', 'value8'));
+ $this->assertTrue($cacheone->set('key8', 'value8'));
}
- $this->assertEquals('value7', $instance->get('key7'), 'Overrides are incorrectly incrementing size');
+ $this->assertEquals('value7', $cacheone->get('key7'), 'Overrides are incorrectly incrementing size');
// Test adding many.
- $this->assertEquals(3, $instance->set_many(array(
- array('key' => 'keyA', 'value' => 'valueA'),
- array('key' => 'keyB', 'value' => 'valueB'),
- array('key' => 'keyC', 'value' => 'valueC')
+ $this->assertEquals(3, $cacheone->set_many(array(
+ 'keyA' => 'valueA',
+ 'keyB' => 'valueB',
+ 'keyC' => 'valueC'
)));
$this->assertEquals(array(
'key4' => false,
'keyA' => 'valueA',
'keyB' => 'valueB',
'keyC' => 'valueC'
- ), $instance->get_many(array(
+ ), $cacheone->get_many(array(
'key4', 'key5', 'key6', 'key7', 'keyA', 'keyB', 'keyC'
)));
+
+ $cachetwo = cache::make('phpunit', 'two');
+
+ // Test adding many.
+ $this->assertEquals(3, $cacheone->set_many(array(
+ 'keyA' => 'valueA',
+ 'keyB' => 'valueB',
+ 'keyC' => 'valueC'
+ )));
+
+ $this->assertEquals(3, $cachetwo->set_many(array(
+ 'key1' => 'value1',
+ 'key2' => 'value2',
+ 'key3' => 'value3'
+ )));
+
+ $this->assertEquals(array(
+ 'keyA' => 'valueA',
+ 'keyB' => 'valueB',
+ 'keyC' => 'valueC'
+ ), $cacheone->get_many(array(
+ 'keyA', 'keyB', 'keyC'
+ )));
+
+ $this->assertEquals(array(
+ 'key1' => 'value1',
+ 'key2' => 'value2',
+ 'key3' => 'value3'
+ ), $cachetwo->get_many(array(
+ 'key1', 'key2', 'key3'
+ )));
+
+ // Test that that cache deletes element that was least recently accessed.
+ $this->assertEquals('valueA', $cacheone->get('keyA'));
+ $cacheone->set('keyD', 'valueD');
+ $this->assertEquals('valueA', $cacheone->get('keyA'));
+ $this->assertFalse($cacheone->get('keyB'));
+ $this->assertEquals(array('keyD' => 'valueD', 'keyC' => 'valueC'), $cacheone->get_many(array('keyD', 'keyC')));
+ $cacheone->set('keyE', 'valueE');
+ $this->assertFalse($cacheone->get('keyB'));
+ $this->assertFalse($cacheone->get('keyA'));
+ $this->assertEquals(array('keyA' => false, 'keyE' => 'valueE', 'keyD' => 'valueD', 'keyC' => 'valueC'),
+ $cacheone->get_many(array('keyA', 'keyE', 'keyD', 'keyC')));
+ // Overwrite keyE (moves it to the end of array), and set keyF.
+ $cacheone->set_many(array('keyE' => 'valueE', 'keyF' => 'valueF'));
+ $this->assertEquals(array('keyC' => 'valueC', 'keyE' => 'valueE', 'keyD' => false, 'keyF' => 'valueF'),
+ $cacheone->get_many(array('keyC', 'keyE', 'keyD', 'keyF')));
+ }
+
+ public function test_ttl() {
+ $config = cache_config_phpunittest::instance();
+ $config->phpunit_add_definition('phpunit/three', array(
+ 'mode' => cache_store::MODE_SESSION,
+ 'component' => 'phpunit',
+ 'area' => 'three',
+ 'maxsize' => 3,
+ 'ttl' => 3
+ ));
+
+ $cachethree = cache::make('phpunit', 'three');
+
+ // Make sure that when cache with ttl is full the elements that were added first are deleted first regardless of access time.
+ $cachethree->set('key1', 'value1');
+ $cachethree->set('key2', 'value2');
+ $cachethree->set('key3', 'value3');
+ $cachethree->set('key4', 'value4');
+ $this->assertFalse($cachethree->get('key1'));
+ $this->assertEquals('value4', $cachethree->get('key4'));
+ $cachethree->set('key5', 'value5');
+ $this->assertFalse($cachethree->get('key2'));
+ $this->assertEquals('value4', $cachethree->get('key4'));
+ $cachethree->set_many(array('key6' => 'value6', 'key7' => 'value7'));
+ $this->assertEquals(array('key3' => false, 'key4' => false, 'key5' => 'value5', 'key6' => 'value6', 'key7' => 'value7'),
+ $cachethree->get_many(array('key3', 'key4', 'key5', 'key6', 'key7')));
}
}
\ No newline at end of file
}
/**
- * Test that multiple loaders work ok.
+ * Test that multiple application loaders work ok.
*/
- public function test_multiple_loaders() {
+ public function test_multiple_application_loaders() {
$instance = cache_config_phpunittest::instance(true);
$instance->phpunit_add_file_store('phpunittest1');
$instance->phpunit_add_file_store('phpunittest2');
$this->assertFalse($result['a']);
$this->assertEquals('B', $result['b']);
$this->assertFalse($result['c']);
+
+ // Test non-recursive deletes.
+ $this->assertTrue($cache->set('test', 'test'));
+ $this->assertSame('test', $cache->get('test'));
+ $this->assertTrue($cache->delete('test', false));
+ // We should still have it on a deeper loader.
+ $this->assertSame('test', $cache->get('test'));
+ // Test non-recusive with many functions.
+ $this->assertSame(3, $cache->set_many(array(
+ 'one' => 'one',
+ 'two' => 'two',
+ 'three' => 'three'
+ )));
+ $this->assertSame('one', $cache->get('one'));
+ $this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
+ $this->assertSame(3, $cache->delete_many(array('one', 'two', 'three'), false));
+ $this->assertSame('one', $cache->get('one'));
+ $this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
+ }
+
+ /**
+ * Test that multiple application loaders work ok.
+ */
+ public function test_multiple_session_loaders() {
+ /* @var cache_config_phpunittest $instance */
+ $instance = cache_config_phpunittest::instance(true);
+ $instance->phpunit_add_session_store('phpunittest1');
+ $instance->phpunit_add_session_store('phpunittest2');
+ $instance->phpunit_add_definition('phpunit/multi_loader', array(
+ 'mode' => cache_store::MODE_SESSION,
+ 'component' => 'phpunit',
+ 'area' => 'multi_loader'
+ ));
+ $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest1', 3);
+ $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest2', 2);
+
+ $cache = cache::make('phpunit', 'multi_loader');
+ $this->assertInstanceOf('cache_session', $cache);
+ $this->assertFalse($cache->get('test'));
+ $this->assertTrue($cache->set('test', 'test'));
+ $this->assertEquals('test', $cache->get('test'));
+ $this->assertTrue($cache->delete('test'));
+ $this->assertFalse($cache->get('test'));
+ $this->assertTrue($cache->set('test', 'test'));
+ $this->assertTrue($cache->purge());
+ $this->assertFalse($cache->get('test'));
+
+ // Test the many commands.
+ $this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C')));
+ $result = $cache->get_many(array('a', 'b', 'c'));
+ $this->assertInternalType('array', $result);
+ $this->assertCount(3, $result);
+ $this->assertArrayHasKey('a', $result);
+ $this->assertArrayHasKey('b', $result);
+ $this->assertArrayHasKey('c', $result);
+ $this->assertEquals('A', $result['a']);
+ $this->assertEquals('B', $result['b']);
+ $this->assertEquals('C', $result['c']);
+ $this->assertEquals($result, $cache->get_many(array('a', 'b', 'c')));
+ $this->assertEquals(2, $cache->delete_many(array('a', 'c')));
+ $result = $cache->get_many(array('a', 'b', 'c'));
+ $this->assertInternalType('array', $result);
+ $this->assertCount(3, $result);
+ $this->assertArrayHasKey('a', $result);
+ $this->assertArrayHasKey('b', $result);
+ $this->assertArrayHasKey('c', $result);
+ $this->assertFalse($result['a']);
+ $this->assertEquals('B', $result['b']);
+ $this->assertFalse($result['c']);
+
+ // Test non-recursive deletes.
+ $this->assertTrue($cache->set('test', 'test'));
+ $this->assertSame('test', $cache->get('test'));
+ $this->assertTrue($cache->delete('test', false));
+ // We should still have it on a deeper loader.
+ $this->assertSame('test', $cache->get('test'));
+ // Test non-recusive with many functions.
+ $this->assertSame(3, $cache->set_many(array(
+ 'one' => 'one',
+ 'two' => 'two',
+ 'three' => 'three'
+ )));
+ $this->assertSame('one', $cache->get('one'));
+ $this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
+ $this->assertSame(3, $cache->delete_many(array('one', 'two', 'three'), false));
+ $this->assertSame('one', $cache->get('one'));
+ $this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
}
/**
$this->assertInstanceOf('cache_request', $cache);
$this->assertArrayHasKey('cache_is_searchable', $cache->phpunit_get_store_implements());
}
-}
+}
\ No newline at end of file
);
}
+ /**
+ * Forcefully adds a session store.
+ *
+ * @param string $name
+ */
+ public function phpunit_add_session_store($name) {
+ $this->configstores[$name] = array(
+ 'name' => $name,
+ 'plugin' => 'session',
+ 'configuration' => array(),
+ 'features' => 14,
+ 'modes' => 2,
+ 'default' => true,
+ 'class' => 'cachestore_session',
+ 'lock' => 'cachelock_file_default',
+ );
+ }
+
/**
* Forcefully injects a definition => store mapping.
*
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
- * DDL layer tests
+ * DDL layer tests.
*
* @package core_ddl
* @category phpunit
protected function setUp() {
parent::setUp();
- $dbman = $this->tdb->get_manager(); // loads DDL libs
+ $dbman = $this->tdb->get_manager(); // Loads DDL libs.
$table = new xmldb_table('test_table0');
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'general');
$table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null);
$table->add_field('intro', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
$table->add_field('logo', XMLDB_TYPE_BINARY, 'big', null, null, null);
- $table->add_field('assessed', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
- $table->add_field('assesstimestart', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
- $table->add_field('assesstimefinish', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $table->add_field('assessed', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
+ $table->add_field('assesstimestart', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
+ $table->add_field('assesstimefinish', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('scale', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
- $table->add_field('maxbytes', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
- $table->add_field('forcesubscribe', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
- $table->add_field('trackingtype', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1');
- $table->add_field('rsstype', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
- $table->add_field('rssarticles', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
- $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
- $table->add_field('grade', XMLDB_TYPE_NUMBER, '20,0', XMLDB_UNSIGNED, null, null, null);
+ $table->add_field('maxbytes', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
+ $table->add_field('forcesubscribe', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
+ $table->add_field('trackingtype', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '1');
+ $table->add_field('rsstype', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0');
+ $table->add_field('rssarticles', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0');
+ $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
+ $table->add_field('grade', XMLDB_TYPE_NUMBER, '20,0', null, null, null, null);
$table->add_field('percent', XMLDB_TYPE_NUMBER, '5,2', null, null, null, 66.6);
- $table->add_field('warnafter', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
- $table->add_field('blockafter', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
- $table->add_field('blockperiod', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $table->add_field('warnafter', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
+ $table->add_field('blockafter', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
+ $table->add_field('blockperiod', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->add_key('course', XMLDB_KEY_UNIQUE, array('course'));
$table->add_index('type-name', XMLDB_INDEX_UNIQUE, array('type', 'name'));
$this->tables[$table->getName()] = $table;
- // Define 2 initial records for this table
+ // Define 2 initial records for this table.
$this->records[$table->getName()] = array(
(object)array(
'course' => '1',
'name' => 'record',
'intro' => 'second record'));
- // Second, smaller table
+ // Second, smaller table.
$table = new xmldb_table ('test_table1');
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('name', XMLDB_TYPE_CHAR, '30', null, null, null, 'Moodle');
$table->add_field('secondname', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null);
- $table->add_field('thirdname', XMLDB_TYPE_CHAR, '30', null, null, null, ''); // nullable column with empty default
+ $table->add_field('thirdname', XMLDB_TYPE_CHAR, '30', null, null, null, ''); // Nullable column with empty default.
$table->add_field('intro', XMLDB_TYPE_TEXT, 'medium', null, XMLDB_NOTNULL, null, null);
$table->add_field('avatar', XMLDB_TYPE_BINARY, 'medium', null, null, null, null);
$table->add_field('grade', XMLDB_TYPE_NUMBER, '20,10', null, null, null);
- $table->add_field('gradefloat', XMLDB_TYPE_FLOAT, '20,0', XMLDB_UNSIGNED, null, null, null);
+ $table->add_field('gradefloat', XMLDB_TYPE_FLOAT, '20,0', null, null, null, null);
$table->add_field('percentfloat', XMLDB_TYPE_FLOAT, '5,2', null, null, null, 99.9);
- $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->add_key('course', XMLDB_KEY_FOREIGN_UNIQUE, array('course'), 'test_table0', array('course'));
$table->setComment("This is a test'n drop table. You can drop it safely");
$this->tables[$table->getName()] = $table;
- // Define 2 initial records for this table
+ // Define 2 initial records for this table.
$this->records[$table->getName()] = array(
(object)array(
'course' => '1',
- 'secondname' => 'first record', // > 10 cc, please don't modify. Some tests below depend of this
+ 'secondname' => 'first record', // Less than 10 cc, please don't modify. Some tests below depend of this.
'intro' => 'first record'),
(object)array(
'course' => '2',
- 'secondname' => 'second record', // > 10 cc, please don't modify. Some tests below depend of this
+ 'secondname' => 'second record', // More than 10 cc, please don't modify. Some tests below depend of this.
'intro' => 'second record'));
}
* @return int count of records
*/
private function fill_deftable($tablename) {
- $DB = $this->tdb; // do not use global $DB!
+ $DB = $this->tdb; // Do not use global $DB!
$dbman = $this->tdb->get_manager();
if (!isset($this->records[$tablename])) {
* Test behaviour of table_exists()
*/
public function test_table_exists() {
- $DB = $this->tdb; // do not use global $DB!
+ $DB = $this->tdb; // Do not use global $DB!
$dbman = $this->tdb->get_manager();
- // first make sure it returns false if table does not exist
+ // First make sure it returns false if table does not exist.
$table = $this->tables['test_table0'];
try {
$this->assertFalse($result);
- $this->assertFalse($dbman->table_exists('test_table0')); // by name
- $this->assertFalse($dbman->table_exists($table)); // by xmldb_table
+ $this->assertFalse($dbman->table_exists('test_table0')); // By name..
+ $this->assertFalse($dbman->table_exists($table)); // By xmldb_table..
- // create table and test again
+ // Create table and test again.
$dbman->create_table($table);
- $this->assertTrue($DB->get_records('test_table0') === array());
- $this->assertTrue($dbman->table_exists('test_table0')); // by name
- $this->assertTrue($dbman->table_exists($table)); // by xmldb_table
+ $this->assertSame(array(), $DB->get_records('test_table0'));
+ $this->assertTrue($dbman->table_exists('test_table0')); // By name.
+ $this->assertTrue($dbman->table_exists($table)); // By xmldb_table.
- // drop table and test again
+ // Drop table and test again.
$dbman->drop_table($table);
try {
$this->assertFalse($result);
- $this->assertFalse($dbman->table_exists('test_table0')); // by name
- $this->assertFalse($dbman->table_exists($table)); // by xmldb_table
+ $this->assertFalse($dbman->table_exists('test_table0')); // By name.
+ $this->assertFalse($dbman->table_exists($table)); // By xmldb_table.
}
/**
* Test behaviour of create_table()
*/
public function test_create_table() {
- $DB = $this->tdb; // do not use global $DB!
+ $DB = $this->tdb; // Do not use global $DB!
$dbman = $this->tdb->get_manager();
- // create table
+ // Create table.
$table = $this->tables['test_table1'];
$dbman->create_table($table);
$this->assertTrue($dbman->table_exists($table));
- // basic get_tables() test
+ // Basic get_tables() test.
$tables = $DB->get_tables();
- $this->assertTrue(array_key_exists('test_table1', $tables));
+ $this->assertArrayHasKey('test_table1', $tables);
- // basic get_columns() tests
+ // Basic get_columns() tests.
$columns = $DB->get_columns('test_table1');
- $this->assertEquals($columns['id']->meta_type, 'R');
- $this->assertEquals($columns['course']->meta_type, 'I');
- $this->assertEquals($columns['name']->meta_type, 'C');
- $this->assertEquals($columns['secondname']->meta_type, 'C');
- $this->assertEquals($columns['thirdname']->meta_type, 'C');
- $this->assertEquals($columns['intro']->meta_type, 'X');
- $this->assertEquals($columns['avatar']->meta_type, 'B');
- $this->assertEquals($columns['grade']->meta_type, 'N');
- $this->assertEquals($columns['percentfloat']->meta_type, 'N');
- $this->assertEquals($columns['userid']->meta_type, 'I');
- // some defaults
+ $this->assertSame('R', $columns['id']->meta_type);
+ $this->assertSame('I', $columns['course']->meta_type);
+ $this->assertSame('C', $columns['name']->meta_type);
+ $this->assertSame('C', $columns['secondname']->meta_type);
+ $this->assertSame('C', $columns['thirdname']->meta_type);
+ $this->assertSame('X', $columns['intro']->meta_type);
+ $this->assertSame('B', $columns['avatar']->meta_type);
+ $this->assertSame('N', $columns['grade']->meta_type);
+ $this->assertSame('N', $columns['percentfloat']->meta_type);
+ $this->assertSame('I', $columns['userid']->meta_type);
+ // Some defaults.
$this->assertTrue($columns['course']->has_default);
- $this->assertEquals($columns['course']->default_value, 0);
+ $this->assertEquals(0, $columns['course']->default_value);
$this->assertTrue($columns['name']->has_default);
- $this->assertEquals($columns['name']->default_value, 'Moodle');
+ $this->assertSame('Moodle', $columns['name']->default_value);
$this->assertTrue($columns['secondname']->has_default);
- $this->assertEquals($columns['secondname']->default_value, '');
+ $this->assertSame('', $columns['secondname']->default_value);
$this->assertTrue($columns['thirdname']->has_default);
- $this->assertEquals($columns['thirdname']->default_value, '');
+ $this->assertSame('', $columns['thirdname']->default_value);
$this->assertTrue($columns['percentfloat']->has_default);
- $this->assertEquals($columns['percentfloat']->default_value, 99.9);
+ $this->assertEquals(99.9, $columns['percentfloat']->default_value);
$this->assertTrue($columns['userid']->has_default);
- $this->assertEquals($columns['userid']->default_value, 0);
+ $this->assertEquals(0, $columns['userid']->default_value);
- // basic get_indexes() test
+ // Basic get_indexes() test.
$indexes = $DB->get_indexes('test_table1');
$courseindex = reset($indexes);
- $this->assertEquals($courseindex['unique'], 1);
- $this->assertEquals($courseindex['columns'][0], 'course');
+ $this->assertEquals(1, $courseindex['unique']);
+ $this->assertSame('course', $courseindex['columns'][0]);
- // check sequence returns 1 for first insert
+ // Check sequence returns 1 for first insert.
$rec = (object)array(
'course' => 10,
'secondname' => 'not important',
'intro' => 'not important');
- $this->assertSame($DB->insert_record('test_table1', $rec), 1);
+ $this->assertSame(1, $DB->insert_record('test_table1', $rec));
- // check defined defaults are working ok
+ // Check defined defaults are working ok.
$dbrec = $DB->get_record('test_table1', array('id' => 1));
- $this->assertEquals($dbrec->name, 'Moodle');
- $this->assertEquals($dbrec->thirdname, '');
+ $this->assertSame('Moodle', $dbrec->name);
+ $this->assertSame('', $dbrec->thirdname);
- // check exceptions if multiple R columns
+ // Check exceptions if multiple R columns.
$table = new xmldb_table ('test_table2');
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('rid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('rid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->add_key('primaryx', XMLDB_KEY_PRIMARY, array('id'));
$table->setComment("This is a test'n drop table. You can drop it safely");
try {
$dbman->create_table($table);
$this->fail('Exception expected');
- } catch (Exception $e) {
- $this->assertTrue($e instanceof ddl_exception);
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('ddl_exception', $e);
}
- // check exceptions missing primary key on R column
+ // Check exceptions missing primary key on R column.
$table = new xmldb_table ('test_table2');
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->setComment("This is a test'n drop table. You can drop it safely");
$this->tables[$table->getName()] = $table;
try {
$dbman->create_table($table);
$this->fail('Exception expected');
- } catch (Exception $e) {
- $this->assertTrue($e instanceof ddl_exception);
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('ddl_exception', $e);
}
- // long table name names - the largest allowed
+ // Long table name names - the largest allowed.
$table = new xmldb_table('test_table0123456789_____xyz');
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '2');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '2');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->setComment("This is a test'n drop table. You can drop it safely");
$this->assertTrue($dbman->table_exists($table));
$dbman->drop_table($table);
- // table name is too long
+ // Table name is too long.
$table = new xmldb_table('test_table0123456789_____xyz9');
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '2');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '2');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->setComment("This is a test'n drop table. You can drop it safely");
try {
$dbman->create_table($table);
$this->fail('Exception expected');
- } catch (Exception $e) {
- $this->assertSame(get_class($e), 'coding_exception');
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('coding_exception', $e);
}
- // invalid table name
+ // Invalid table name.
$table = new xmldb_table('test_tableCD');
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '2');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '2');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->setComment("This is a test'n drop table. You can drop it safely");
try {
$dbman->create_table($table);
$this->fail('Exception expected');
- } catch (Exception $e) {
- $this->assertSame(get_class($e), 'coding_exception');
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('coding_exception', $e);
}
-
- // weird column names - the largest allowed
+ // Weird column names - the largest allowed.
$table = new xmldb_table('test_table3');
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('abcdef____0123456789_______xyz', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '2');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('abcdef____0123456789_______xyz', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '2');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->setComment("This is a test'n drop table. You can drop it safely");
$this->assertTrue($dbman->table_exists($table));
$dbman->drop_table($table);
- // Too long field name - max 30
+ // Too long field name - max 30.
$table = new xmldb_table('test_table4');
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('abcdeabcdeabcdeabcdeabcdeabcdez', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '2');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('abcdeabcdeabcdeabcdeabcdeabcdez', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '2');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->setComment("This is a test'n drop table. You can drop it safely");
try {
$dbman->create_table($table);
$this->fail('Exception expected');
- } catch (Exception $e) {
- $this->assertSame(get_class($e), 'coding_exception');
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('coding_exception', $e);
}
- // Invalid field name
+ // Invalid field name.
$table = new xmldb_table('test_table4');
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('abCD', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '2');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('abCD', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '2');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->setComment("This is a test'n drop table. You can drop it safely");
try {
$dbman->create_table($table);
$this->fail('Exception expected');
- } catch (Exception $e) {
- $this->assertSame(get_class($e), 'coding_exception');
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('coding_exception', $e);
}
- // Invalid integer length
+ // Invalid integer length.
$table = new xmldb_table('test_table4');
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '21', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '2');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '21', null, XMLDB_NOTNULL, null, '2');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->setComment("This is a test'n drop table. You can drop it safely");
try {
$dbman->create_table($table);
$this->fail('Exception expected');
- } catch (Exception $e) {
- $this->assertSame(get_class($e), 'coding_exception');
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('coding_exception', $e);
}
- // Invalid integer default
+ // Invalid integer default.
$table = new xmldb_table('test_table4');
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 'x');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, 'x');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->setComment("This is a test'n drop table. You can drop it safely");
try {
$dbman->create_table($table);
$this->fail('Exception expected');
- } catch (Exception $e) {
- $this->assertSame(get_class($e), 'coding_exception');
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('coding_exception', $e);
}
- // Invalid decimal length
+ // Invalid decimal length.
$table = new xmldb_table('test_table4');
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('num', XMLDB_TYPE_NUMBER, '21,10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('num', XMLDB_TYPE_NUMBER, '21,10', null, XMLDB_NOTNULL, null, null);
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->setComment("This is a test'n drop table. You can drop it safely");
try {
$dbman->create_table($table);
$this->fail('Exception expected');
- } catch (Exception $e) {
- $this->assertSame(get_class($e), 'coding_exception');
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('coding_exception', $e);
}
- // Invalid decimal decimals
+ // Invalid decimal decimals.
$table = new xmldb_table('test_table4');
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('num', XMLDB_TYPE_NUMBER, '10,11', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('num', XMLDB_TYPE_NUMBER, '10,11', null, XMLDB_NOTNULL, null, null);
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->setComment("This is a test'n drop table. You can drop it safely");
try {
$dbman->create_table($table);
$this->fail('Exception expected');
- } catch (Exception $e) {
- $this->assertSame(get_class($e), 'coding_exception');
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('coding_exception', $e);
}
- // Invalid decimal default
+ // Invalid decimal default.
$table = new xmldb_table('test_table4');
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('num', XMLDB_TYPE_NUMBER, '10,5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 'x');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('num', XMLDB_TYPE_NUMBER, '10,5', null, XMLDB_NOTNULL, null, 'x');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->setComment("This is a test'n drop table. You can drop it safely");
try {
$dbman->create_table($table);
$this->fail('Exception expected');
- } catch (Exception $e) {
- $this->assertSame(get_class($e), 'coding_exception');
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('coding_exception', $e);
}
- // Invalid float length
+ // Invalid float length.
$table = new xmldb_table('test_table4');
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('num', XMLDB_TYPE_FLOAT, '21,10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('num', XMLDB_TYPE_FLOAT, '21,10', null, XMLDB_NOTNULL, null, null);
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->setComment("This is a test'n drop table. You can drop it safely");
try {
$dbman->create_table($table);
$this->fail('Exception expected');
- } catch (Exception $e) {
- $this->assertSame(get_class($e), 'coding_exception');
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('coding_exception', $e);
}
- // Invalid float decimals
+ // Invalid float decimals.
$table = new xmldb_table('test_table4');
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('num', XMLDB_TYPE_FLOAT, '10,11', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('num', XMLDB_TYPE_FLOAT, '10,11', null, XMLDB_NOTNULL, null, null);
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->setComment("This is a test'n drop table. You can drop it safely");
try {
$dbman->create_table($table);
$this->fail('Exception expected');
- } catch (Exception $e) {
- $this->assertSame(get_class($e), 'coding_exception');
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('coding_exception', $e);
}
- // Invalid float default
+ // Invalid float default.
$table = new xmldb_table('test_table4');
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('num', XMLDB_TYPE_FLOAT, '10,5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 'x');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('num', XMLDB_TYPE_FLOAT, '10,5', null, XMLDB_NOTNULL, null, 'x');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->setComment("This is a test'n drop table. You can drop it safely");
try {
$dbman->create_table($table);
$this->fail('Exception expected');
- } catch (Exception $e) {
- $this->assertSame(get_class($e), 'coding_exception');
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('coding_exception', $e);
}
-
}
/**
* Test behaviour of drop_table()
*/
public function test_drop_table() {
- $DB = $this->tdb; // do not use global $DB!
+ $DB = $this->tdb; // Do not use global $DB!
$dbman = $this->tdb->get_manager();
- // initially table doesn't exist
+ // Initially table doesn't exist.
$this->assertFalse($dbman->table_exists('test_table0'));
- // create table with contents
+ // Create table with contents.
$table = $this->create_deftable('test_table0');
$this->assertTrue($dbman->table_exists('test_table0'));
- // fill the table with some records before dropping it
+ // Fill the table with some records before dropping it.
$this->fill_deftable('test_table0');
- // drop by xmldb_table object
+ // Drop by xmldb_table object.
$dbman->drop_table($table);
$this->assertFalse($dbman->table_exists('test_table0'));
- // basic get_tables() test
+ // Basic get_tables() test.
$tables = $DB->get_tables();
- $this->assertFalse(array_key_exists('test_table0', $tables));
+ $this->assertArrayNotHasKey('test_table0', $tables);
- // columns cache must be empty
+ // Columns cache must be empty.
$columns = $DB->get_columns('test_table0');
$this->assertEmpty($columns);
* Test behaviour of rename_table()
*/
public function test_rename_table() {
- $DB = $this->tdb; // do not use global $DB!
+ $DB = $this->tdb; // Do not use global $DB!
$dbman = $this->tdb->get_manager();
$table = $this->create_deftable('test_table1');
- // fill the table with some records before renaming it
+ // Fill the table with some records before renaming it.
$insertedrows = $this->fill_deftable('test_table1');
$this->assertFalse($dbman->table_exists('test_table_cust1'));
$dbman->rename_table($table, 'test_table_cust1');
$this->assertTrue($dbman->table_exists('test_table_cust1'));
- // check sequence returns $insertedrows + 1 for this insert (after rename)
+ // Check sequence returns $insertedrows + 1 for this insert (after rename).
$rec = (object)array(
'course' => 20,
'secondname' => 'not important',
'intro' => 'not important');
- $this->assertSame($DB->insert_record('test_table_cust1', $rec), $insertedrows + 1);
+ $this->assertSame($insertedrows+1, $DB->insert_record('test_table_cust1', $rec));
}
/**
$table = $this->create_deftable('test_table0');
- // String params
- // Give a nonexistent table as first param (throw exception)
+ // String params.
+ // Give a nonexistent table as first param (throw exception).
try {
$dbman->field_exists('nonexistenttable', 'id');
- $this->assertTrue(false);
- } catch (Exception $e) {
- $this->assertTrue($e instanceof moodle_exception);
+ $this->fail('Exception expected');
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('moodle_exception', $e);
}
- // Give a nonexistent field as second param (return false)
+ // Give a nonexistent field as second param (return false).
$this->assertFalse($dbman->field_exists('test_table0', 'nonexistentfield'));
- // Correct string params
+ // Correct string params.
$this->assertTrue($dbman->field_exists('test_table0', 'id'));
- // Object params
+ // Object params.
$realfield = $table->getField('id');
- // Give a nonexistent table as first param (throw exception)
+ // Give a nonexistent table as first param (throw exception).
$nonexistenttable = new xmldb_table('nonexistenttable');
try {
$dbman->field_exists($nonexistenttable, $realfield);
- $this->assertTrue(false);
- } catch (Exception $e) {
- $this->assertTrue($e instanceof moodle_exception);
+ $this->fail('Exception expected');
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('moodle_exception', $e);
}
- // Give a nonexistent field as second param (return false)
+ // Give a nonexistent field as second param (return false).
$nonexistentfield = new xmldb_field('nonexistentfield');
$this->assertFalse($dbman->field_exists($table, $nonexistentfield));
- // Correct object params
+ // Correct object params.
$this->assertTrue($dbman->field_exists($table, $realfield));
- // Mix string and object params
- // Correct ones
+ // Mix string and object params.
+ // Correct ones.
$this->assertTrue($dbman->field_exists($table, 'id'));
$this->assertTrue($dbman->field_exists('test_table0', $realfield));
- // Non existing tables (throw exception)
+ // Non existing tables (throw exception).
try {
$this->assertFalse($dbman->field_exists($nonexistenttable, 'id'));
- $this->assertTrue(false);
- } catch (Exception $e) {
- $this->assertTrue($e instanceof moodle_exception);
+ $this->fail('Exception expected');
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('moodle_exception', $e);
}
try {
$this->assertFalse($dbman->field_exists('nonexistenttable', $realfield));
- $this->assertTrue(false);
- } catch (Exception $e) {
- $this->assertTrue($e instanceof moodle_exception);
+ $this->fail('Exception expected');
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('moodle_exception', $e);
}
- // Non existing fields (return false)
+ // Non existing fields (return false).
$this->assertFalse($dbman->field_exists($table, 'nonexistentfield'));
$this->assertFalse($dbman->field_exists('test_table0', $nonexistentfield));
}
* Test behaviour of add_field()
*/
public function test_add_field() {
- $DB = $this->tdb; // do not use global $DB!
+ $DB = $this->tdb; // Do not use global $DB!
$dbman = $this->tdb->get_manager();
$table = $this->create_deftable('test_table1');
- // fill the table with some records before adding fields
+ // Fill the table with some records before adding fields.
$this->fill_deftable('test_table1');
- // add one not null field without specifying default value (throws ddl_exception)
+ // Add one not null field without specifying default value (throws ddl_exception).
$field = new xmldb_field('onefield');
- $field->set_attributes(XMLDB_TYPE_INTEGER, '6', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
+ $field->set_attributes(XMLDB_TYPE_INTEGER, '6', null, XMLDB_NOTNULL, null, null);
try {
$dbman->add_field($table, $field);
- $this->assertTrue(false);
- } catch (Exception $e) {
- $this->assertTrue($e instanceof ddl_exception);
+ $this->fail('Exception expected');
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('ddl_exception', $e);
}
- // add one existing field (throws ddl_exception)
+ // Add one existing field (throws ddl_exception).
$field = new xmldb_field('course');
- $field->set_attributes(XMLDB_TYPE_INTEGER, '6', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 2);
+ $field->set_attributes(XMLDB_TYPE_INTEGER, '6', null, XMLDB_NOTNULL, null, 2);
try {
$dbman->add_field($table, $field);
- $this->assertTrue(false);
- } catch (Exception $e) {
- $this->assertTrue($e instanceof ddl_exception);
+ $this->fail('Exception expected');
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('ddl_exception', $e);
}
- // TODO: add one field with invalid type, must throw exception
- // TODO: add one text field with default, must throw exception
- // TODO: add one binary field with default, must throw exception
+ // TODO: add one field with invalid type, must throw exception.
+ // TODO: add one text field with default, must throw exception.
+ // TODO: add one binary field with default, must throw exception.
- // add one integer field and check it
+ // Add one integer field and check it.
$field = new xmldb_field('oneinteger');
- $field->set_attributes(XMLDB_TYPE_INTEGER, '6', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 2);
+ $field->set_attributes(XMLDB_TYPE_INTEGER, '6', null, XMLDB_NOTNULL, null, 2);
$dbman->add_field($table, $field);
$this->assertTrue($dbman->field_exists($table, 'oneinteger'));
$columns = $DB->get_columns('test_table1');
- $this->assertEquals($columns['oneinteger']->name ,'oneinteger');
- $this->assertEquals($columns['oneinteger']->not_null , true);
- // max_length and scale cannot be checked under all DBs at all for integer fields
- $this->assertEquals($columns['oneinteger']->primary_key , false);
- $this->assertEquals($columns['oneinteger']->binary , false);
- $this->assertEquals($columns['oneinteger']->has_default , true);
- $this->assertEquals($columns['oneinteger']->default_value, 2);
- $this->assertEquals($columns['oneinteger']->meta_type ,'I');
- $this->assertEquals($DB->get_field('test_table1', 'oneinteger', array(), IGNORE_MULTIPLE), 2); //check default has been applied
-
- // add one numeric field and check it
+ $this->assertEquals('oneinteger', $columns['oneinteger']->name);
+ $this->assertTrue($columns['oneinteger']->not_null);
+ // Max_length and scale cannot be checked under all DBs at all for integer fields.
+ $this->assertFalse($columns['oneinteger']->primary_key);
+ $this->assertFalse($columns['oneinteger']->binary);
+ $this->assertTrue($columns['oneinteger']->has_default);
+ $this->assertEquals(2, $columns['oneinteger']->default_value);
+ $this->assertSame('I', $columns['oneinteger']->meta_type);
+ $this->assertEquals(2, $DB->get_field('test_table1', 'oneinteger', array(), IGNORE_MULTIPLE)); // Check default has been applied.
+
+ // Add one numeric field and check it.
$field = new xmldb_field('onenumber');
- $field->set_attributes(XMLDB_TYPE_NUMBER, '6,3', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 2.55);
+ $field->set_attributes(XMLDB_TYPE_NUMBER, '6,3', null, XMLDB_NOTNULL, null, 2.55);
$dbman->add_field($table, $field);
$this->assertTrue($dbman->field_exists($table, 'onenumber'));
$columns = $DB->get_columns('test_table1');
- $this->assertEquals($columns['onenumber']->name ,'onenumber');
- $this->assertEquals($columns['onenumber']->max_length , 6);
- $this->assertEquals($columns['onenumber']->scale , 3);
- $this->assertEquals($columns['onenumber']->not_null , true);
- $this->assertEquals($columns['onenumber']->primary_key , false);
- $this->assertEquals($columns['onenumber']->binary , false);
- $this->assertEquals($columns['onenumber']->has_default , true);
- $this->assertEquals($columns['onenumber']->default_value, 2.550);
- $this->assertEquals($columns['onenumber']->meta_type ,'N');
- $this->assertEquals($DB->get_field('test_table1', 'onenumber', array(), IGNORE_MULTIPLE), 2.550); //check default has been applied
-
- // add one float field and check it (not official type - must work as number)
+ $this->assertSame('onenumber', $columns['onenumber']->name);
+ $this->assertEquals(6, $columns['onenumber']->max_length);
+ $this->assertEquals(3, $columns['onenumber']->scale);
+ $this->assertTrue($columns['onenumber']->not_null);
+ $this->assertFalse($columns['onenumber']->primary_key);
+ $this->assertFalse($columns['onenumber']->binary);
+ $this->assertTrue($columns['onenumber']->has_default);
+ $this->assertEquals(2.550, $columns['onenumber']->default_value);
+ $this->assertSame('N', $columns['onenumber']->meta_type);
+ $this->assertEquals(2.550, $DB->get_field('test_table1', 'onenumber', array(), IGNORE_MULTIPLE)); // Check default has been applied.
+
+ // Add one float field and check it (not official type - must work as number).
$field = new xmldb_field('onefloat');
- $field->set_attributes(XMLDB_TYPE_FLOAT, '6,3', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 3.550);
+ $field->set_attributes(XMLDB_TYPE_FLOAT, '6,3', null, XMLDB_NOTNULL, null, 3.550);
$dbman->add_field($table, $field);
$this->assertTrue($dbman->field_exists($table, 'onefloat'));
$columns = $DB->get_columns('test_table1');
- $this->assertEquals($columns['onefloat']->name ,'onefloat');
- $this->assertEquals($columns['onefloat']->not_null , true);
- // max_length and scale cannot be checked under all DBs at all for float fields
- $this->assertEquals($columns['onefloat']->primary_key , false);
- $this->assertEquals($columns['onefloat']->binary , false);
- $this->assertEquals($columns['onefloat']->has_default , true);
- $this->assertEquals($columns['onefloat']->default_value, 3.550);
- $this->assertEquals($columns['onefloat']->meta_type ,'N');
+ $this->assertSame('onefloat', $columns['onefloat']->name);
+ $this->assertTrue($columns['onefloat']->not_null);
+ // Max_length and scale cannot be checked under all DBs at all for float fields.
+ $this->assertFalse($columns['onefloat']->primary_key);
+ $this->assertFalse($columns['onefloat']->binary);
+ $this->assertTrue($columns['onefloat']->has_default);
+ $this->assertEquals(3.550, $columns['onefloat']->default_value);
+ $this->assertSame('N', $columns['onefloat']->meta_type);
// Just rounding DB information to 7 decimal digits. Fair enough to test 3.550 and avoids one nasty bug
// in MSSQL core returning wrong floats (http://social.msdn.microsoft.com/Forums/en-US/sqldataaccess/thread/5e08de63-16bb-4f24-b645-0cf8fc669de3)
// In any case, floats aren't officially supported by Moodle, with number/decimal type being the correct ones, so
// this isn't a real problem at all.
- $this->assertEquals(round($DB->get_field('test_table1', 'onefloat', array(), IGNORE_MULTIPLE), 7), 3.550); //check default has been applied
+ $this->assertEquals(3.550, round($DB->get_field('test_table1', 'onefloat', array(), IGNORE_MULTIPLE), 7)); // Check default has been applied.
- // add one char field and check it
+ // Add one char field and check it.
$field = new xmldb_field('onechar');
- $field->set_attributes(XMLDB_TYPE_CHAR, '25', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 'Nice dflt!');
+ $field->set_attributes(XMLDB_TYPE_CHAR, '25', null, XMLDB_NOTNULL, null, 'Nice dflt!');
$dbman->add_field($table, $field);
$this->assertTrue($dbman->field_exists($table, 'onechar'));
$columns = $DB->get_columns('test_table1');
- $this->assertEquals($columns['onechar']->name ,'onechar');
- $this->assertEquals($columns['onechar']->max_length , 25);
- $this->assertEquals($columns['onechar']->scale , null);
- $this->assertEquals($columns['onechar']->not_null , true);
- $this->assertEquals($columns['onechar']->primary_key , false);
- $this->assertEquals($columns['onechar']->binary , false);
- $this->assertEquals($columns['onechar']->has_default , true);
- $this->assertEquals($columns['onechar']->default_value,'Nice dflt!');
- $this->assertEquals($columns['onechar']->meta_type ,'C');
- $this->assertEquals($DB->get_field('test_table1', 'onechar', array(), IGNORE_MULTIPLE), 'Nice dflt!'); //check default has been applied
-
- // add one big text field and check it
+ $this->assertSame('onechar', $columns['onechar']->name);
+ $this->assertEquals(25, $columns['onechar']->max_length);
+ $this->assertNull($columns['onechar']->scale);
+ $this->assertTrue($columns['onechar']->not_null);
+ $this->assertFalse($columns['onechar']->primary_key);
+ $this->assertFalse($columns['onechar']->binary);
+ $this->assertTrue($columns['onechar']->has_default);
+ $this->assertSame('Nice dflt!', $columns['onechar']->default_value);
+ $this->assertSame('C', $columns['onechar']->meta_type);
+ $this->assertEquals('Nice dflt!', $DB->get_field('test_table1', 'onechar', array(), IGNORE_MULTIPLE)); // Check default has been applied.
+
+ // Add one big text field and check it.
$field = new xmldb_field('onetext');
$field->set_attributes(XMLDB_TYPE_TEXT, 'big');
$dbman->add_field($table, $field);
$this->assertTrue($dbman->field_exists($table, 'onetext'));
$columns = $DB->get_columns('test_table1');
- $this->assertEquals($columns['onetext']->name ,'onetext');
- $this->assertEquals($columns['onetext']->max_length , -1); // -1 means unknown or big
- $this->assertEquals($columns['onetext']->scale , null);
- $this->assertEquals($columns['onetext']->not_null , false);
- $this->assertEquals($columns['onetext']->primary_key , false);
- $this->assertEquals($columns['onetext']->binary , false);
- $this->assertEquals($columns['onetext']->has_default , false);
- $this->assertEquals($columns['onetext']->default_value, null);
- $this->assertEquals($columns['onetext']->meta_type ,'X');
-
- // add one medium text field and check it
+ $this->assertSame('onetext', $columns['onetext']->name);
+ $this->assertEquals(-1, $columns['onetext']->max_length); // -1 means unknown or big.
+ $this->assertNull($columns['onetext']->scale);
+ $this->assertFalse($columns['onetext']->not_null);
+ $this->assertFalse($columns['onetext']->primary_key);
+ $this->assertFalse($columns['onetext']->binary);
+ $this->assertFalse($columns['onetext']->has_default);
+ $this->assertNull($columns['onetext']->default_value);
+ $this->assertSame('X', $columns['onetext']->meta_type);
+
+ // Add one medium text field and check it.
$field = new xmldb_field('mediumtext');
$field->set_attributes(XMLDB_TYPE_TEXT, 'medium');
$dbman->add_field($table, $field);
$columns = $DB->get_columns('test_table1');
- $this->assertTrue(($columns['mediumtext']->max_length == -1) or ($columns['mediumtext']->max_length >= 16777215)); // -1 means unknown or big
+ $this->assertTrue(($columns['mediumtext']->max_length == -1) or ($columns['mediumtext']->max_length >= 16777215)); // -1 means unknown or big.
- // add one small text field and check it
+ // Add one small text field and check it.
$field = new xmldb_field('smalltext');
$field->set_attributes(XMLDB_TYPE_TEXT, 'small');
$dbman->add_field($table, $field);
$columns = $DB->get_columns('test_table1');
- $this->assertTrue(($columns['smalltext']->max_length == -1) or ($columns['smalltext']->max_length >= 65535)); // -1 means unknown or big
+ $this->assertTrue(($columns['smalltext']->max_length == -1) or ($columns['smalltext']->max_length >= 65535)); // -1 means unknown or big.
- // add one binary field and check it
+ // Add one binary field and check it.
$field = new xmldb_field('onebinary');
$field->set_attributes(XMLDB_TYPE_BINARY);
$dbman->add_field($table, $field);
$this->assertTrue($dbman->field_exists($table, 'onebinary'));
$columns = $DB->get_columns('test_table1');
- $this->assertEquals($columns['onebinary']->name ,'onebinary');
- $this->assertEquals($columns['onebinary']->max_length , -1);
- $this->assertEquals($columns['onebinary']->scale , null);
- $this->assertEquals($columns['onebinary']->not_null , false);
- $this->assertEquals($columns['onebinary']->primary_key , false);
- $this->assertEquals($columns['onebinary']->binary , true);
- $this->assertEquals($columns['onebinary']->has_default , false);
- $this->assertEquals($columns['onebinary']->default_value, null);
- $this->assertEquals($columns['onebinary']->meta_type ,'B');
+ $this->assertSame('onebinary', $columns['onebinary']->name);
+ $this->assertEquals(-1, $columns['onebinary']->max_length);
+ $this->assertNull($columns['onebinary']->scale);
+ $this->assertFalse($columns['onebinary']->not_null);
+ $this->assertFalse($columns['onebinary']->primary_key);
+ $this->assertTrue($columns['onebinary']->binary);
+ $this->assertFalse($columns['onebinary']->has_default);
+ $this->assertNull($columns['onebinary']->default_value);
+ $this->assertSame('B', $columns['onebinary']->meta_type);
// TODO: check datetime type. Although unused should be fully supported.
}
* Test behaviour of drop_field()
*/
public function test_drop_field() {
- $DB = $this->tdb; // do not use global $DB!
+ $DB = $this->tdb; // Do not use global $DB!
$dbman = $this->tdb->get_manager();
$table = $this->create_deftable('test_table0');
- // fill the table with some records before dropping fields
+ // Fill the table with some records before dropping fields.
$this->fill_deftable('test_table0');
- // drop field with simple xmldb_field having indexes, must return exception
- $field = new xmldb_field('type'); // Field has indexes and default clause
+ // Drop field with simple xmldb_field having indexes, must return exception.
+ $field = new xmldb_field('type'); // Field has indexes and default clause.
$this->assertTrue($dbman->field_exists($table, 'type'));
try {
$dbman->drop_field($table, $field);
- $this->assertTrue(false);
- } catch (Exception $e) {
- $this->assertTrue($e instanceof ddl_dependency_exception);
+ $this->fail('Exception expected');
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('ddl_dependency_exception', $e);
}
- $this->assertTrue($dbman->field_exists($table, 'type')); // continues existing, drop aborted
+ $this->assertTrue($dbman->field_exists($table, 'type')); // Continues existing, drop aborted.
- // drop field with complete xmldb_field object and related indexes, must return exception
- $field = $table->getField('course'); // Field has indexes and default clause
+ // Drop field with complete xmldb_field object and related indexes, must return exception.
+ $field = $table->getField('course'); // Field has indexes and default clause.
$this->assertTrue($dbman->field_exists($table, $field));
try {
$dbman->drop_field($table, $field);
- $this->assertTrue(false);
- } catch (Exception $e) {
- $this->assertTrue($e instanceof ddl_dependency_exception);
+ $this->fail('Exception expected');
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('ddl_dependency_exception', $e);
}
- $this->assertTrue($dbman->field_exists($table, $field)); // continues existing, drop aborted
+ $this->assertTrue($dbman->field_exists($table, $field)); // Continues existing, drop aborted.
- // drop one non-existing field, must return exception
+ // Drop one non-existing field, must return exception.
$field = new xmldb_field('nonexistingfield');
$this->assertFalse($dbman->field_exists($table, $field));
try {
$dbman->drop_field($table, $field);
- $this->assertTrue(false);
- } catch (Exception $e) {
- $this->assertTrue($e instanceof ddl_field_missing_exception);
+ $this->fail('Exception expected');
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('ddl_field_missing_exception', $e);
}
- // drop field with simple xmldb_field, not having related indexes
- $field = new xmldb_field('forcesubscribe'); // Field has default clause
+ // Drop field with simple xmldb_field, not having related indexes.
+ $field = new xmldb_field('forcesubscribe'); // Field has default clause.
$this->assertTrue($dbman->field_exists($table, 'forcesubscribe'));
$dbman->drop_field($table, $field);
$this->assertFalse($dbman->field_exists($table, 'forcesubscribe'));
-
- // drop field with complete xmldb_field object, not having related indexes
- $field = new xmldb_field('trackingtype'); // Field has default clause
+ // Drop field with complete xmldb_field object, not having related indexes.
+ $field = new xmldb_field('trackingtype'); // Field has default clause.
$this->assertTrue($dbman->field_exists($table, $field));
$dbman->drop_field($table, $field);
$this->assertFalse($dbman->field_exists($table, $field));
* Test behaviour of change_field_type()
*/
public function test_change_field_type() {
- $DB = $this->tdb; // do not use global $DB!
+ $DB = $this->tdb; // Do not use global $DB!
$dbman = $this->tdb->get_manager();
- // create table with indexed field and not indexed field to
- // perform tests in both fields, both having defaults
+ // Create table with indexed field and not indexed field to
+ // perform tests in both fields, both having defaults.
$table = new xmldb_table('test_table_cust0');
- $table->add_field('id', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('onenumber', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '2');
- $table->add_field('anothernumber', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '4');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('onenumber', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '2');
+ $table->add_field('anothernumber', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '4');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->add_index('onenumber', XMLDB_INDEX_NOTUNIQUE, array('onenumber'));
$dbman->create_table($table);
$record->anothernumber = 4;
$recoriginal = $DB->insert_record('test_table_cust0', $record);
- // change column from integer to varchar. Must return exception because of dependent index
+ // Change column from integer to varchar. Must return exception because of dependent index.
$field = new xmldb_field('onenumber');
$field->set_attributes(XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, 'test');
try {
$dbman->change_field_type($table, $field);
- $this->assertTrue(false);
- } catch (Exception $e) {
- $this->assertTrue($e instanceof ddl_dependency_exception);
+ $this->fail('Exception expected');
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('ddl_dependency_exception', $e);
}
- // column continues being integer 10 not null default 2
+ // Column continues being integer 10 not null default 2.
$columns = $DB->get_columns('test_table_cust0');
- $this->assertEquals($columns['onenumber']->meta_type, 'I');
- //TODO: check the rest of attributes
+ $this->assertSame('I', $columns['onenumber']->meta_type);
+ // TODO: check the rest of attributes.
- // change column from integer to varchar. Must work because column has no dependencies
+ // Change column from integer to varchar. Must work because column has no dependencies.
$field = new xmldb_field('anothernumber');
$field->set_attributes(XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, 'test');
$dbman->change_field_type($table, $field);
- // column is char 30 not null default 'test' now
+ // Column is char 30 not null default 'test' now.
$columns = $DB->get_columns('test_table_cust0');
- $this->assertEquals($columns['anothernumber']->meta_type, 'C');
- //TODO: check the rest of attributes
+ $this->assertSame('C', $columns['anothernumber']->meta_type);
+ // TODO: check the rest of attributes.
- // change column back from char to integer
+ // Change column back from char to integer.
$field = new xmldb_field('anothernumber');
- $field->set_attributes(XMLDB_TYPE_INTEGER, '8', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '5');
+ $field->set_attributes(XMLDB_TYPE_INTEGER, '8', null, XMLDB_NOTNULL, null, '5');
$dbman->change_field_type($table, $field);
- // column is integer 8 not null default 5 now
+ // Column is integer 8 not null default 5 now.
$columns = $DB->get_columns('test_table_cust0');
- $this->assertEquals($columns['anothernumber']->meta_type, 'I');
- //TODO: check the rest of attributes
+ $this->assertSame('I', $columns['anothernumber']->meta_type);
+ // TODO: check the rest of attributes.
- // change column once more from integer to char
+ // Change column once more from integer to char.
$field = new xmldb_field('anothernumber');
$field->set_attributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, "test'n drop");
$dbman->change_field_type($table, $field);
- // column is char 30 not null default "test'n drop" now
+ // Column is char 30 not null default "test'n drop" now.
$columns = $DB->get_columns('test_table_cust0');
- $this->assertEquals($columns['anothernumber']->meta_type, 'C');
- //TODO: check the rest of attributes
+ $this->assertSame('C', $columns['anothernumber']->meta_type);
+ // TODO: check the rest of attributes.
- // insert one string value and try to convert to integer. Must throw exception
+ // Insert one string value and try to convert to integer. Must throw exception.
$record = new stdClass();
$record->onenumber = 7;
$record->anothernumber = 'string value';
$rectodrop = $DB->insert_record('test_table_cust0', $record);
$field = new xmldb_field('anothernumber');
- $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '5');
+ $field->set_attributes(XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '5');
try {
$dbman->change_field_type($table, $field);
- $this->assertTrue(false);
- } catch (Exception $e) {
- $this->assertTrue($e instanceof ddl_change_structure_exception);
+ $this->fail('Exception expected');
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('ddl_change_structure_exception', $e);
}
- // column continues being char 30 not null default "test'n drop" now
- $this->assertEquals($columns['anothernumber']->meta_type, 'C');
- //TODO: check the rest of attributes
- $DB->delete_records('test_table_cust0', array('id' => $rectodrop)); // Delete the string record
+ // Column continues being char 30 not null default "test'n drop" now.
+ $this->assertSame('C', $columns['anothernumber']->meta_type);
+ // TODO: check the rest of attributes.
+ $DB->delete_records('test_table_cust0', array('id' => $rectodrop)); // Delete the string record.
- // change the column from varchar to float
+ // Change the column from varchar to float.
$field = new xmldb_field('anothernumber');
- $field->set_attributes(XMLDB_TYPE_FLOAT, '20,10', XMLDB_UNSIGNED, null, null, null);
+ $field->set_attributes(XMLDB_TYPE_FLOAT, '20,10', null, null, null, null);
$dbman->change_field_type($table, $field);
- // column is float 20,10 null default null
+ // Column is float 20,10 null default null.
$columns = $DB->get_columns('test_table_cust0');
- $this->assertEquals($columns['anothernumber']->meta_type, 'N'); // floats are seen as number
- //TODO: check the rest of attributes
+ $this->assertSame('N', $columns['anothernumber']->meta_type); // Floats are seen as number.
+ // TODO: check the rest of attributes.
- // change the column back from float to varchar
+ // Change the column back from float to varchar.
$field = new xmldb_field('anothernumber');
$field->set_attributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'test');
$dbman->change_field_type($table, $field);
- // column is char 20 not null default "test" now
+ // Column is char 20 not null default "test" now.
$columns = $DB->get_columns('test_table_cust0');
- $this->assertEquals($columns['anothernumber']->meta_type, 'C');
- //TODO: check the rest of attributes
+ $this->assertSame('C', $columns['anothernumber']->meta_type);
+ // TODO: check the rest of attributes.
- // change the column from varchar to number
+ // Change the column from varchar to number.
$field = new xmldb_field('anothernumber');
- $field->set_attributes(XMLDB_TYPE_NUMBER, '20,10', XMLDB_UNSIGNED, null, null, null);
+ $field->set_attributes(XMLDB_TYPE_NUMBER, '20,10', null, null, null, null);
$dbman->change_field_type($table, $field);
- // column is number 20,10 null default null now
+ // Column is number 20,10 null default null now.
$columns = $DB->get_columns('test_table_cust0');
- $this->assertEquals($columns['anothernumber']->meta_type, 'N');
- //TODO: check the rest of attributes
+ $this->assertSame('N', $columns['anothernumber']->meta_type);
+ // TODO: check the rest of attributes.
- // change the column from number to integer
+ // Change the column from number to integer.
$field = new xmldb_field('anothernumber');
- $field->set_attributes(XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, null, null, null);
+ $field->set_attributes(XMLDB_TYPE_INTEGER, '2', null, null, null, null);
$dbman->change_field_type($table, $field);
- // column is integer 2 null default null now
+ // Column is integer 2 null default null now.
$columns = $DB->get_columns('test_table_cust0');
- $this->assertEquals($columns['anothernumber']->meta_type, 'I');
- //TODO: check the rest of attributes
+ $this->assertSame('I', $columns['anothernumber']->meta_type);
+ // TODO: check the rest of attributes.
- // change the column from integer to text
+ // Change the column from integer to text.
$field = new xmldb_field('anothernumber');
$field->set_attributes(XMLDB_TYPE_TEXT, 'big', null, XMLDB_NOTNULL, null, null);
$dbman->change_field_type($table, $field);
- // column is char text not null default null
+ // Column is char text not null default null.
$columns = $DB->get_columns('test_table_cust0');
- $this->assertEquals($columns['anothernumber']->meta_type, 'X');
+ $this->assertSame('X', $columns['anothernumber']->meta_type);
- // change the column back from text to number
+ // Change the column back from text to number.
$field = new xmldb_field('anothernumber');
- $field->set_attributes(XMLDB_TYPE_NUMBER, '20,10', XMLDB_UNSIGNED, null, null, null);
+ $field->set_attributes(XMLDB_TYPE_NUMBER, '20,10', null, null, null, null);
$dbman->change_field_type($table, $field);
- // column is number 20,10 null default null now
+ // Column is number 20,10 null default null now.
$columns = $DB->get_columns('test_table_cust0');
- $this->assertEquals($columns['anothernumber']->meta_type, 'N');
- //TODO: check the rest of attributes
+ $this->assertSame('N', $columns['anothernumber']->meta_type);
+ // TODO: check the rest of attributes.
- // change the column from number to text
+ // Change the column from number to text.
$field = new xmldb_field('anothernumber');
$field->set_attributes(XMLDB_TYPE_TEXT, 'big', null, XMLDB_NOTNULL, null, null);
$dbman->change_field_type($table, $field);
- // column is char text not null default "test" now
+ // Column is char text not null default "test" now.
$columns = $DB->get_columns('test_table_cust0');
- $this->assertEquals($columns['anothernumber']->meta_type, 'X');
- //TODO: check the rest of attributes
+ $this->assertSame('X', $columns['anothernumber']->meta_type);
+ // TODO: check the rest of attributes.
- // change the column back from text to integer
+ // Change the column back from text to integer.
$field = new xmldb_field('anothernumber');
- $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 10);
+ $field->set_attributes(XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, 10);
$dbman->change_field_type($table, $field);
- // column is integer 10 not null default 10
+ // Column is integer 10 not null default 10.
$columns = $DB->get_columns('test_table_cust0');
- $this->assertEquals($columns['anothernumber']->meta_type, 'I');
- //TODO: check the rest of attributes
+ $this->assertSame('I', $columns['anothernumber']->meta_type);
+ // TODO: check the rest of attributes.
- // check original value has survived to all the type changes
+ // Check original value has survived to all the type changes.
$this->assertnotEmpty($rec = $DB->get_record('test_table_cust0', array('id' => $recoriginal)));
- $this->assertEquals($rec->anothernumber, 4);
+ $this->assertEquals(4, $rec->anothernumber);
$dbman->drop_table($table);
$this->assertFalse($dbman->table_exists($table));
* Test behaviour of test_change_field_precision()
*/
public function test_change_field_precision() {
- $DB = $this->tdb; // do not use global $DB!
+ $DB = $this->tdb; // Do not use global $DB!
$dbman = $this->tdb->get_manager();
$table = $this->create_deftable('test_table1');
- // fill the table with some records before dropping fields
+ // Fill the table with some records before dropping fields.
$this->fill_deftable('test_table1');
- // change text field from medium to big
+ // Change text field from medium to big.
$field = new xmldb_field('intro');
$field->set_attributes(XMLDB_TYPE_TEXT, 'big', null, XMLDB_NOTNULL, null, null);
$dbman->change_field_precision($table, $field);
$columns = $DB->get_columns('test_table1');
- // cannot check the text type, only the metatype
- $this->assertEquals($columns['intro']->meta_type, 'X');
- //TODO: check the rest of attributes
+ // Cannot check the text type, only the metatype.
+ $this->assertSame('X', $columns['intro']->meta_type);
+ // TODO: check the rest of attributes.
- // change char field from 30 to 20
+ // Change char field from 30 to 20.
$field = new xmldb_field('secondname');
$field->set_attributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
$dbman->change_field_precision($table, $field);
$columns = $DB->get_columns('test_table1');
- $this->assertEquals($columns['secondname']->meta_type, 'C');
- //TODO: check the rest of attributes
+ $this->assertSame('C', $columns['secondname']->meta_type);
+ // TODO: check the rest of attributes.
- // change char field from 20 to 10, having contents > 10cc. Throw exception
+ // Change char field from 20 to 10, having contents > 10cc. Throw exception.
$field = new xmldb_field('secondname');
$field->set_attributes(XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null);
try {
$dbman->change_field_precision($table, $field);
- $this->assertTrue(false);
- } catch (Exception $e) {
- $this->assertTrue($e instanceof ddl_change_structure_exception);
+ $this->fail('Exception expected');
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('ddl_change_structure_exception', $e);
}
- // No changes in field specs at all
+ // No changes in field specs at all.
$columns = $DB->get_columns('test_table1');
- $this->assertEquals($columns['secondname']->meta_type, 'C');
- //TODO: check the rest of attributes
+ $this->assertSame('C', $columns['secondname']->meta_type);
+ // TODO: check the rest of attributes.
- // change number field from 20,10 to 10,2
+ // Change number field from 20,10 to 10,2.
$field = new xmldb_field('grade');
$field->set_attributes(XMLDB_TYPE_NUMBER, '10,2', null, null, null, null);
$dbman->change_field_precision($table, $field);
$columns = $DB->get_columns('test_table1');
- $this->assertEquals($columns['grade']->meta_type, 'N');
- //TODO: check the rest of attributes
+ $this->assertSame('N', $columns['grade']->meta_type);
+ // TODO: check the rest of attributes.
- // change integer field from 10 to 2
+ // Change integer field from 10 to 2.
$field = new xmldb_field('userid');
- $field->set_attributes(XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $field->set_attributes(XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0');
$dbman->change_field_precision($table, $field);
$columns = $DB->get_columns('test_table1');
- $this->assertEquals($columns['userid']->meta_type, 'I');
- //TODO: check the rest of attributes
+ $this->assertSame('I', $columns['userid']->meta_type);
+ // TODO: check the rest of attributes.
- // change the column from integer (2) to integer (6) (forces change of type in some DBs)
+ // Change the column from integer (2) to integer (6) (forces change of type in some DBs).
$field = new xmldb_field('userid');
- $field->set_attributes(XMLDB_TYPE_INTEGER, '6', XMLDB_UNSIGNED, null, null, null);
+ $field->set_attributes(XMLDB_TYPE_INTEGER, '6', null, null, null, null);
$dbman->change_field_precision($table, $field);
- // column is integer 6 null default null now
+ // Column is integer 6 null default null now.
$columns = $DB->get_columns('test_table1');
- $this->assertEquals($columns['userid']->meta_type, 'I');
- //TODO: check the rest of attributes
+ $this->assertSame('I', $columns['userid']->meta_type);
+ // TODO: check the rest of attributes.
- // insert one record with 6-digit field
+ // Insert one record with 6-digit field.
$record = new stdClass();
$record->course = 10;
$record->secondname = 'third record';
$record->intro = 'third record';
$record->userid = 123456;
$DB->insert_record('test_table1', $record);
- // change integer field from 6 to 2, contents are bigger. must throw exception
+ // Change integer field from 6 to 2, contents are bigger, must throw exception.
$field = new xmldb_field('userid');
- $field->set_attributes(XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $field->set_attributes(XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0');
try {
$dbman->change_field_precision($table, $field);
- $this->assertTrue(false);
- } catch (Exception $e) {
- $this->assertTrue($e instanceof ddl_change_structure_exception);
+ $this->fail('Exception expected');
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('ddl_change_structure_exception', $e);
}
- // No changes in field specs at all
+ // No changes in field specs at all.
$columns = $DB->get_columns('test_table1');
- $this->assertEquals($columns['userid']->meta_type, 'I');
- //TODO: check the rest of attributes
+ $this->assertSame('I', $columns['userid']->meta_type);
+ // TODO: check the rest of attributes.
- // change integer field from 10 to 3, in field used by index. must throw exception.
+ // Change integer field from 10 to 3, in field used by index. must throw exception.
$field = new xmldb_field('course');
- $field->set_attributes(XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $field->set_attributes(XMLDB_TYPE_INTEGER, '3', null, XMLDB_NOTNULL, null, '0');
try {
$dbman->change_field_precision($table, $field);
- $this->assertTrue(false);
- } catch (Exception $e) {
- $this->assertTrue($e instanceof ddl_dependency_exception);
+ $this->fail('Exception expected');
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('ddl_dependency_exception', $e);
}
- // No changes in field specs at all
+ // No changes in field specs at all.
$columns = $DB->get_columns('test_table1');
- $this->assertEquals($columns['course']->meta_type, 'I');
- //TODO: check the rest of attributes
+ $this->assertSame('I', $columns['course']->meta_type);
+ // TODO: check the rest of attributes.
}
public function testChangeFieldNullability() {
- $DB = $this->tdb; // do not use global $DB!
+ $DB = $this->tdb; // Do not use global $DB!
$dbman = $this->tdb->get_manager();
$table = new xmldb_table('test_table_cust0');
- $table->add_field('id', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('name', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null);
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$dbman->create_table($table);
$record = new stdClass();
- $record->name = NULL;
+ $record->name = null;
try {
$result = $DB->insert_record('test_table_cust0', $record, false);
$this->assertTrue($DB->insert_record('test_table_cust0', $record, false));
- // TODO: add some tests with existing data in table
+ // TODO: add some tests with existing data in table.
$DB->delete_records('test_table_cust0');
$field = new xmldb_field('name');
}
public function testChangeFieldDefault() {
- $DB = $this->tdb; // do not use global $DB!
+ $DB = $this->tdb; // Do not use global $DB!
$dbman = $this->tdb->get_manager();
$table = new xmldb_table('test_table_cust0');
- $table->add_field('id', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('onenumber', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('onenumber', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('name', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, 'Moodle');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$dbman->create_table($table);
$id = $DB->insert_record('test_table_cust0', $record);
$record = $DB->get_record('test_table_cust0', array('id'=>$id));
- $this->assertEquals($record->name, 'Moodle2');
-
+ $this->assertSame('Moodle2', $record->name);
$field = new xmldb_field('onenumber');
- $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 666);
+ $field->set_attributes(XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, 666);
$dbman->change_field_default($table, $field);
$record = new stdClass();
$id = $DB->insert_record('test_table_cust0', $record);
$record = $DB->get_record('test_table_cust0', array('id'=>$id));
- $this->assertEquals($record->onenumber, '666');
+ $this->assertSame('666', $record->onenumber);
$dbman->drop_table($table);
}
public function testAddUniqueIndex() {
- $DB = $this->tdb; // do not use global $DB!
+ $DB = $this->tdb; // Do not use global $DB!
$dbman = $this->tdb->get_manager();
$table = new xmldb_table('test_table_cust0');
- $table->add_field('id', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('onenumber', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('onenumber', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('name', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, 'Moodle');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$dbman->create_table($table);
try {
$dbman->add_index($table, $index);
$this->fail('Exception expected for duplicate indexes');
- } catch (Exception $e) {
+ } catch (moodle_exception $e) {
$this->assertInstanceOf('ddl_exception', $e);
}
try {
$dbman->add_index($table, $index);
$this->fail('Exception expected for duplicate indexes');
- } catch (Exception $e) {
+ } catch (moodle_exception $e) {
$this->assertInstanceOf('ddl_exception', $e);
}
try {
$table->add_index('onenumber', XMLDB_INDEX_NOTUNIQUE, array('onenumber'));
$this->fail('Coding exception expected');
- } catch (Exception $e) {
+ } catch (moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
try {
$table->add_key('onenumber', XMLDB_KEY_FOREIGN, array('onenumber'));
$this->fail('Coding exception expected');
- } catch (Exception $e) {
+ } catch (moodle_exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
$index->set_attributes(XMLDB_INDEX_NOTUNIQUE, array('course', 'name'));
$dbman->add_index($table, $index);
- //DBM Systems name their indices differently - do not test the actual index name
+ // DBM Systems name their indices differently - do not test the actual index name.
$result = $dbman->find_index_name($table, $index);
$this->assertTrue(!empty($result));
}
public function testDropIndex() {
- $DB = $this->tdb; // do not use global $DB!
+ $DB = $this->tdb; // Do not use global $DB!
$dbman = $this->tdb->get_manager();
$dbman->drop_index($table, $index);
$this->assertFalse($dbman->find_index_name($table, $index));
- // Test we are able to drop indexes having hyphens. MDL-22804
- // Create index with hyphens (by hand)
+ // Test we are able to drop indexes having hyphens MDL-22804.
+ // Create index with hyphens (by hand).
$indexname = 'test-index-with-hyphens';
switch ($DB->get_dbfamily()) {
case 'mysql':
$stmt = "CREATE INDEX {$indexname} ON {$DB->get_prefix()}test_table1 (course, name)";
$DB->change_database_structure($stmt);
$this->assertNotEmpty($dbman->find_index_name($table, $index));
- // Index created, let's drop it using db manager stuff
+ // Index created, let's drop it using db manager stuff.
$index = new xmldb_index('indexname', XMLDB_INDEX_NOTUNIQUE, array('course', 'name'));
$dbman->drop_index($table, $index);
$this->assertFalse($dbman->find_index_name($table, $index));
}
public function testRenameField() {
- $DB = $this->tdb; // do not use global $DB!
+ $DB = $this->tdb; // Do not use global $DB!
$dbman = $this->tdb->get_manager();
$table = $this->create_deftable('test_table0');
$columns = $DB->get_columns('test_table0');
- $this->assertFalse(array_key_exists('type', $columns));
- $this->assertTrue(array_key_exists('newfieldname', $columns));
+ $this->assertArrayNotHasKey('type', $columns);
+ $this->assertArrayHasKey('newfieldname', $columns);
}
public function testIndexExists() {
- // Skipping: this is just a test of find_index_name
+ // Skipping: this is just a test of find_index_name.
}
public function testFindKeyName() {
$table = $this->create_deftable('test_table0');
$key = $table->getKey('primary');
- // With Mysql, the return value is actually "mdl_test_id_pk"
+ // With Mysql, the return value is actually "mdl_test_id_pk".
$result = $dbman->find_key_name($table, $key);
$this->assertTrue(!empty($result));
}
public function testDeleteTablesFromXmldbFile() {
- global $CFG;
$dbman = $this->tdb->get_manager();
$this->create_deftable('test_table1');
$this->assertTrue($dbman->table_exists('test_table1'));
- // feed nonexistent file
+ // Feed nonexistent file.
try {
$dbman->delete_tables_from_xmldb_file('fpsoiudfposui');
- $this->assertTrue(false);
- } catch (Exception $e) {
+ $this->fail('Exception expected');
+ } catch (moodle_exception $e) {
$this->resetDebugging();
- $this->assertTrue($e instanceof moodle_exception);
+ $this->assertInstanceOf('moodle_exception', $e);
}
try {
$dbman->delete_tables_from_xmldb_file(__DIR__ . '/fixtures/invalid.xml');
- $this->assertTrue(false);
- } catch (Exception $e) {
+ $this->fail('Exception expected');
+ } catch (moodle_exception $e) {
$this->resetDebugging();
- $this->assertTrue($e instanceof moodle_exception);
+ $this->assertInstanceOf('moodle_exception', $e);
}
- // Check that the table has not been deleted from DB
+ // Check that the table has not been deleted from DB.
$this->assertTrue($dbman->table_exists('test_table1'));
- // Real and valid xml file
- //TODO: drop UNSINGED completely in Moodle 2.4
+ // Real and valid xml file.
+ // TODO: drop UNSINGED completely in Moodle 2.4.
$dbman->delete_tables_from_xmldb_file(__DIR__ . '/fixtures/xmldb_table.xml');
- // Check that the table has been deleted from DB
+ // Check that the table has been deleted from DB.
$this->assertFalse($dbman->table_exists('test_table1'));
}
public function testInstallFromXmldbFile() {
- global $CFG;
$dbman = $this->tdb->get_manager();
- // feed nonexistent file
+ // Feed nonexistent file.
try {
$dbman->install_from_xmldb_file('fpsoiudfposui');
- $this->assertTrue(false);
- } catch (Exception $e) {
+ $this->fail('Exception expected');
+ } catch (moodle_exception $e) {
$this->resetDebugging();
- $this->assertTrue($e instanceof moodle_exception);
+ $this->assertInstanceOf('moodle_exception', $e);
}
try {
$dbman->install_from_xmldb_file(__DIR__ . '/fixtures/invalid.xml');
- $this->assertTrue(false);
- } catch (Exception $e) {
+ $this->fail('Exception expected');
+ } catch (moodle_exception $e) {
$this->resetDebugging();
- $this->assertTrue($e instanceof moodle_exception);
+ $this->assertInstanceOf('moodle_exception', $e);
}
- // Check that the table has not yet been created in DB
+ // Check that the table has not yet been created in DB.
$this->assertFalse($dbman->table_exists('test_table1'));
- // Real and valid xml file
+ // Real and valid xml file.
$dbman->install_from_xmldb_file(__DIR__ . '/fixtures/xmldb_table.xml');
$this->assertTrue($dbman->table_exists('test_table1'));
}
public function test_temp_tables() {
- global $CFG;
-
- $DB = $this->tdb; // do not use global $DB!
+ $DB = $this->tdb; // Do not use global $DB!
$dbman = $this->tdb->get_manager();
- // Create temp table0
+ // Create temp table0.
$table0 = $this->tables['test_table0'];
$dbman->create_temp_table($table0);
$this->assertTrue($dbman->table_exists('test_table0'));
- // Try to create temp table with same name, must throw exception
+ // Try to create temp table with same name, must throw exception.
$dupetable = $this->tables['test_table0'];
try {
$dbman->create_temp_table($dupetable);
- $this->assertTrue(false);
- } catch (Exception $e) {
- $this->assertTrue($e instanceof ddl_exception);
+ $this->fail('Exception expected');
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('ddl_exception', $e);
}
- // Try to create table with same name, must throw exception
+ // Try to create table with same name, must throw exception.
$dupetable = $this->tables['test_table0'];
try {
$dbman->create_table($dupetable);
- $this->assertTrue(false);
- } catch (Exception $e) {
- $this->assertTrue($e instanceof ddl_exception);
+ $this->fail('Exception expected');
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('ddl_exception', $e);
}
- // Create another temp table1
+ // Create another temp table1.
$table1 = $this->tables['test_table1'];
$dbman->create_temp_table($table1);
$this->assertTrue($dbman->table_exists('test_table1'));
- // Get columns and perform some basic tests
+ // Get columns and perform some basic tests.
$columns = $DB->get_columns('test_table1');
- $this->assertEquals(count($columns), 11);
+ $this->assertCount(11, $columns);
$this->assertTrue($columns['name'] instanceof database_column_info);
- $this->assertEquals($columns['name']->max_length, 30);
+ $this->assertEquals(30, $columns['name']->max_length);
$this->assertTrue($columns['name']->has_default);
- $this->assertEquals($columns['name']->default_value, 'Moodle');
+ $this->assertEquals('Moodle', $columns['name']->default_value);
- // Insert some records
+ // Insert some records.
$inserted = $this->fill_deftable('test_table1');
$records = $DB->get_records('test_table1');
- $this->assertEquals(count($records), $inserted);
- $this->assertEquals($records[1]->course, $this->records['test_table1'][0]->course);
- $this->assertEquals($records[1]->secondname, $this->records['test_table1'][0]->secondname);
- $this->assertEquals($records[2]->intro, $this->records['test_table1'][1]->intro);
+ $this->assertCount($inserted, $records);
+ $this->assertSame($records[1]->course, $this->records['test_table1'][0]->course);
+ $this->assertSame($records[1]->secondname, $this->records['test_table1'][0]->secondname);
+ $this->assertSame($records[2]->intro, $this->records['test_table1'][1]->intro);
- // Drop table1
+ // Drop table1.
$dbman->drop_table($table1);
$this->assertFalse($dbman->table_exists('test_table1'));
- // Try to drop non-existing temp table, must throw exception
+ // Try to drop non-existing temp table, must throw exception.
$noetable = $this->tables['test_table1'];
try {
$dbman->drop_table($noetable);
- $this->assertTrue(false);
- } catch (Exception $e) {
- $this->assertTrue($e instanceof ddl_table_missing_exception);
+ $this->fail('Exception expected');
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('ddl_table_missing_exception', $e);
}
- // Fill/modify/delete a few table0 records
- // TODO: that's
+ // Fill/modify/delete a few table0 records.
- // Drop table0
+ // Drop table0.
$dbman->drop_table($table0);
$this->assertFalse($dbman->table_exists('test_table0'));
- // Create another temp table1
+ // Create another temp table1.
$table1 = $this->tables['test_table1'];
$dbman->create_temp_table($table1);
$this->assertTrue($dbman->table_exists('test_table1'));
- // Make sure it can be dropped using deprecated drop_temp_table()
+ // Make sure it can be dropped using deprecated drop_temp_table().
$dbman->drop_temp_table($table1);
$this->assertFalse($dbman->table_exists('test_table1'));
$this->assertDebuggingCalled();
}
public function test_concurrent_temp_tables() {
- $DB = $this->tdb; // do not use global $DB!
+ $DB = $this->tdb; // Do not use global $DB!
$dbman = $this->tdb->get_manager();
- // Define 2 records
+ // Define 2 records.
$record1 = (object)array(
'course' => 1,
'secondname' => '11 important',
'secondname' => '22 important',
'intro' => '222 important');
- // Create temp table1 and insert 1 record (in DB)
+ // Create temp table1 and insert 1 record (in DB).
$table = $this->tables['test_table1'];
$dbman->create_temp_table($table);
$this->assertTrue($dbman->table_exists('test_table1'));
$inserted = $DB->insert_record('test_table1', $record1);
- // Switch to new connection
+ // Switch to new connection.
$cfg = $DB->export_dbconfig();
if (!isset($cfg->dboptions)) {
$cfg->dboptions = array();
$DB2 = moodle_database::get_driver_instance($cfg->dbtype, $cfg->dblibrary);
$DB2->connect($cfg->dbhost, $cfg->dbuser, $cfg->dbpass, $cfg->dbname, $cfg->prefix, $cfg->dboptions);
$dbman2 = $DB2->get_manager();
- $this->assertFalse($dbman2->table_exists('test_table1')); // Temp table not exists in DB2
+ $this->assertFalse($dbman2->table_exists('test_table1')); // Temp table not exists in DB2.
- // Create temp table1 and insert 1 record (in DB2)
+ // Create temp table1 and insert 1 record (in DB2).
$table = $this->tables['test_table1'];
$dbman2->create_temp_table($table);
$this->assertTrue($dbman2->table_exists('test_table1'));
$inserted = $DB2->insert_record('test_table1', $record2);
- $dbman2->drop_table($table); // Drop temp table before closing DB2
+ $dbman2->drop_table($table); // Drop temp table before closing DB2.
$this->assertFalse($dbman2->table_exists('test_table1'));
- $DB2->dispose(); // Close DB2
+ $DB2->dispose(); // Close DB2.
- $this->assertTrue($dbman->table_exists('test_table1')); // Check table continues existing for DB
- $dbman->drop_table($table); // Drop temp table
+ $this->assertTrue($dbman->table_exists('test_table1')); // Check table continues existing for DB.
+ $dbman->drop_table($table); // Drop temp table.
$this->assertFalse($dbman->table_exists('test_table1'));
}
$dbman = $DB->get_manager();
$table = new xmldb_table('testtable');
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
- // Drop if exists
+ // Drop if exists.
if ($dbman->table_exists($table)) {
$dbman->drop_table($table);
}
$record = (object)array('id'=>666, 'course'=>10);
$DB->import_record('testtable', $record);
- $DB->delete_records('testtable'); // This delete performs one TRUNCATE
+ $DB->delete_records('testtable'); // This delete performs one TRUNCATE.
- $dbman->reset_sequence($table); // using xmldb object
+ $dbman->reset_sequence($table); // Using xmldb object.
$this->assertEquals(1, $DB->insert_record('testtable', (object)array('course'=>13)));
$record = (object)array('id'=>666, 'course'=>10);
$DB->import_record('testtable', $record);
- $DB->delete_records('testtable', array()); // This delete performs one DELETE
+ $DB->delete_records('testtable', array()); // This delete performs one DELETE.
- $dbman->reset_sequence($table); // using xmldb object
+ $dbman->reset_sequence($table); // Using xmldb object.
$this->assertEquals(1, $DB->insert_record('testtable', (object)array('course'=>13)));
$DB->import_record('testtable', $record);
- $dbman->reset_sequence($tablename); // using string
+ $dbman->reset_sequence($tablename); // Using string.
$this->assertEquals(667, $DB->insert_record('testtable', (object)array('course'=>13)));
$dbman->drop_table($table);
$dbman = $DB->get_manager();
$table = new xmldb_table('testtable');
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('name', XMLDB_TYPE_CHAR, 255, null, XMLDB_NOTNULL, null);
$table->add_field('path', XMLDB_TYPE_CHAR, 255, null, XMLDB_NOTNULL, null);
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->add_index('name', XMLDB_INDEX_NOTUNIQUE, array('name'), array('xxxx,yyyy'));
$table->add_index('path', XMLDB_INDEX_NOTUNIQUE, array('path'), array('varchar_pattern_ops'));
- // Drop if exists
+ // Drop if exists.
if ($dbman->table_exists($table)) {
$dbman->drop_table($table);
}
$dbman->drop_table($this->tables[$tablename]);
$table = new xmldb_table('testtable');
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('path', XMLDB_TYPE_CHAR, 255, null, XMLDB_NOTNULL, null);
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->add_index('path', XMLDB_INDEX_UNIQUE, array('path'), array('varchar_pattern_ops'));
$dbman = $DB->get_manager();
$maxstr = '';
- for($i=0; $i<255; $i++) {
- $maxstr .= '言'; // random long string that should fix exactly the limit for one char column
+ for ($i=0; $i<255; $i++) {
+ $maxstr .= '言'; // Random long string that should fix exactly the limit for one char column.
}
$table = new xmldb_table('testtable');
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('name', XMLDB_TYPE_CHAR, 255, null, XMLDB_NOTNULL, null);
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->add_index('name', XMLDB_INDEX_NOTUNIQUE, array('name'));
- // Drop if exists
+ // Drop if exists.
if ($dbman->table_exists($table)) {
$dbman->drop_table($table);
}
$this->assertTrue(!empty($id));
$rec = $DB->get_record($tablename, array('id'=>$id));
- $this->assertSame($rec->name, $maxstr);
+ $this->assertSame($maxstr, $rec->name);
$dbman->drop_table($table);
-
$table = new xmldb_table('testtable');
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('name', XMLDB_TYPE_CHAR, 255+1, null, XMLDB_NOTNULL, null);
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->add_index('name', XMLDB_INDEX_NOTUNIQUE, array('name'));
try {
$dbman->create_table($table);
- $this->assertTrue(false);
- } catch (Exception $e) {
- $this->assertTrue($e instanceof coding_exception);
+ $this->fail('Exception expected');
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('coding_exception', $e);
}
}
$dbman = $DB->get_manager();
$maxstr = '';
- for($i=0; $i<200; $i++) {
+ for ($i=0; $i<200; $i++) {
$maxstr .= '言';
}
$reststr = '';
- for($i=0; $i<133; $i++) {
+ for ($i=0; $i<133; $i++) {
$reststr .= '言';
}
$table = new xmldb_table('testtable');
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('name1', XMLDB_TYPE_CHAR, 200, null, XMLDB_NOTNULL, null);
$table->add_field('name2', XMLDB_TYPE_CHAR, 133, null, XMLDB_NOTNULL, null);
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
- $table->add_index('name1-name2', XMLDB_INDEX_NOTUNIQUE, array('name1','name2'));
+ $table->add_index('name1-name2', XMLDB_INDEX_NOTUNIQUE, array('name1', 'name2'));
- // Drop if exists
+ // Drop if exists.
if ($dbman->table_exists($table)) {
$dbman->drop_table($table);
}
$this->assertTrue(!empty($id));
$rec = $DB->get_record($tablename, array('id'=>$id));
- $this->assertSame($rec->name1, $maxstr);
- $this->assertSame($rec->name2, $reststr);
-
+ $this->assertSame($maxstr, $rec->name1);
+ $this->assertSame($reststr, $rec->name2);
$table = new xmldb_table('testtable');
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('name1', XMLDB_TYPE_CHAR, 201, null, XMLDB_NOTNULL, null);
$table->add_field('name2', XMLDB_TYPE_CHAR, 133, null, XMLDB_NOTNULL, null);
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
- $table->add_index('name1-name2', XMLDB_INDEX_NOTUNIQUE, array('name1','name2'));
+ $table->add_index('name1-name2', XMLDB_INDEX_NOTUNIQUE, array('name1', 'name2'));
- // Drop if exists
+ // Drop if exists.
if ($dbman->table_exists($table)) {
$dbman->drop_table($table);
}
try {
$dbman->create_table($table);
- $this->assertTrue(false);
- } catch (Exception $e) {
- $this->assertTrue($e instanceof coding_exception);
+ $this->fail('Exception expected');
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('coding_exception', $e);
}
}
$dbman = $DB->get_manager();
$table = new xmldb_table('testtable');
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('name', XMLDB_TYPE_CHAR, xmldb_field::CHAR_MAX_LENGTH, null, XMLDB_NOTNULL, null);
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
- // Drop if exists
+ // Drop if exists.
if ($dbman->table_exists($table)) {
$dbman->drop_table($table);
}
$tablename = $table->getName();
$this->tables[$tablename] = $table;
- // this has to work in all DBs
+ // This has to work in all DBs.
$maxstr = '';
- for($i=0; $i<xmldb_field::CHAR_MAX_LENGTH; $i++) {
- $maxstr .= 'a'; // ascii only
+ for ($i=0; $i<xmldb_field::CHAR_MAX_LENGTH; $i++) {
+ $maxstr .= 'a'; // Ascii only.
}
$rec = new stdClass();
$this->assertTrue(!empty($id));
$rec = $DB->get_record($tablename, array('id'=>$id));
- $this->assertSame($rec->name, $maxstr);
-
+ $this->assertSame($maxstr, $rec->name);
- // Following test is supposed to fail in oracle
+ // Following test is supposed to fail in oracle.
$maxstr = '';
- for($i=0; $i<xmldb_field::CHAR_MAX_LENGTH; $i++) {
- $maxstr .= '言'; // random long string that should fix exactly the limit for one char column
+ for ($i=0; $i<xmldb_field::CHAR_MAX_LENGTH; $i++) {
+ $maxstr .= '言'; // Random long string that should fix exactly the limit for one char column.
}
$rec = new stdClass();
$this->assertTrue(!empty($id));
$rec = $DB->get_record($tablename, array('id'=>$id));
- $this->assertSame($rec->name, $maxstr);
+ $this->assertSame($maxstr, $rec->name);
} catch (dml_exception $e) {
if ($DB->get_dbfamily() === 'oracle') {
$this->fail('Oracle does not support text fields larger than 4000 bytes, this is not a big problem for mostly ascii based languages');
}
}
-
$table = new xmldb_table('testtable');
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('name', XMLDB_TYPE_CHAR, xmldb_field::CHAR_MAX_LENGTH+1, null, XMLDB_NOTNULL, null);
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
- // Drop if exists
+ // Drop if exists.
if ($dbman->table_exists($table)) {
$dbman->drop_table($table);
}
try {
$dbman->create_table($table);
- $this->assertTrue(false);
- } catch (Exception $e) {
- $this->assertTrue($e instanceof coding_exception);
+ $this->fail('Exception expected');
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('coding_exception', $e);
}
}
- // Following methods are not supported == Do not test
+ // Following methods are not supported == Do not test.
/*
public function testRenameIndex() {
- // unsupported!
+ // Unsupported!
$dbman = $this->tdb->get_manager();
$table = $this->create_deftable('test_table0');
}
public function testRenameKey() {
- //unsupported
+ // Unsupported!
$dbman = $this->tdb->get_manager();
$table = $this->create_deftable('test_table0');
<TABLES>
<TABLE NAME="test_table1" COMMENT="Just a test table">
<FIELDS>
- <FIELD NAME="id" LENGTH="10" NOTNULL="true" SEQUENCE="true" NEXT="incorrect_next_field"/> <!-- missing TYPE -->
- <FIELD NAME="course" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" PREVIOUS="id" NEXT="name"/>
- <FIELD NAME="name" TYPE="char" LENGTH="30" NOTNULL="false" SEQUENCE="false" DEFAULT="Moodle" PREVIOUS="course" NEXT="secondname"/>
- <FIELD NAME="secondname" TYPE="char" LENGTH="30" NOTNULL="true" SEQUENCE="false" PREVIOUS="name" NEXT="intro"/>
- <FIELD NAME="intro" TYPE="text" LENGTH="medium" NOTNULL="true" SEQUENCE="false" PREVIOUS="secondname" NEXT="avatar"/>
- <FIELD NAME="avatar" TYPE="binary" LENGTH="medium" NOTNULL="false" SEQUENCE="false" PREVIOUS="intro" NEXT="grade"/>
- <FIELD NAME="grade" TYPE="number" LENGTH="20" DECIMALS="10" NOTNULL="false" SEQUENCE="false" PREVIOUS="avatar"/>
+ <FIELD NAME="id" LENGTH="10" NOTNULL="true" SEQUENCE="true"/> <!-- missing TYPE -->
+ <FIELD NAME="course" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
+ <FIELD NAME="name" TYPE="char" LENGTH="30" NOTNULL="false" SEQUENCE="false" DEFAULT="Moodle"/>
+ <FIELD NAME="secondname" TYPE="char" LENGTH="30" NOTNULL="true" SEQUENCE="false"/>
+ <FIELD NAME="intro" TYPE="text" LENGTH="medium" NOTNULL="true" SEQUENCE="false"/>
+ <FIELD NAME="avatar" TYPE="binary" LENGTH="medium" NOTNULL="false" SEQUENCE="false"/>
+ <FIELD NAME="grade" TYPE="number" LENGTH="20" DECIMALS="10" NOTNULL="false" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" />
<TABLES>
<TABLE NAME="test_table1" COMMENT="Just a test table">
<FIELDS>
- <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" NEXT="course"/>
- <FIELD NAME="course" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" PREVIOUS="id" NEXT="name"/>
- <FIELD NAME="name" TYPE="char" LENGTH="30" NOTNULL="false" SEQUENCE="false" DEFAULT="Moodle" PREVIOUS="course" NEXT="secondname"/>
- <FIELD NAME="secondname" TYPE="char" LENGTH="30" NOTNULL="true" SEQUENCE="false" PREVIOUS="name" NEXT="intro"/>
- <FIELD NAME="intro" TYPE="text" LENGTH="medium" NOTNULL="true" SEQUENCE="false" PREVIOUS="secondname" NEXT="avatar"/>
- <FIELD NAME="avatar" TYPE="binary" LENGTH="medium" NOTNULL="false" UNSIGNED="false" SEQUENCE="false" PREVIOUS="intro" NEXT="grade"/>
- <FIELD NAME="grade" TYPE="number" LENGTH="20" DECIMALS="10" NOTNULL="false" SEQUENCE="false" PREVIOUS="avatar" NEXT="path"/>
- <FIELD NAME="path" TYPE="char" LENGTH="255" NOTNULL="true" PREVIOUS="grade"/>
+ <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="true"/>
+ <FIELD NAME="course" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
+ <FIELD NAME="name" TYPE="char" LENGTH="30" NOTNULL="false" SEQUENCE="false" DEFAULT="Moodle"/>
+ <FIELD NAME="secondname" TYPE="char" LENGTH="30" NOTNULL="true" SEQUENCE="false"/>
+ <FIELD NAME="intro" TYPE="text" LENGTH="medium" NOTNULL="true" SEQUENCE="false"/>
+ <FIELD NAME="avatar" TYPE="binary" LENGTH="medium" NOTNULL="false" UNSIGNED="false" SEQUENCE="false"/>
+ <FIELD NAME="grade" TYPE="number" LENGTH="20" DECIMALS="10" NOTNULL="false" SEQUENCE="false"/>
+ <FIELD NAME="path" TYPE="char" LENGTH="255" NOTNULL="true"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" />
</KEYS>
<INDEXES>
- <INDEX NAME="course" UNIQUE="false" FIELDS="course" NEXT="path"/>
- <INDEX NAME="path" UNIQUE="false" FIELDS="path" HINTS="varchar_pattern_ops,unknownxyz" PREVIOUS="course"/>
+ <INDEX NAME="course" UNIQUE="false" FIELDS="course"/>
+ <INDEX NAME="path" UNIQUE="false" FIELDS="path" HINTS="varchar_pattern_ops,unknownxyz"/>
</INDEXES>
</TABLE>
</TABLES>
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
- * DML layer tests
+ * DML layer tests.
*
* @package core_dml
* @category phpunit
protected function setUp() {
parent::setUp();
- $dbman = $this->tdb->get_manager(); // loads DDL libs
+ $dbman = $this->tdb->get_manager(); // Loads DDL libs.
}
/**
return new xmldb_table($tablename);
}
- function test_diagnose() {
+ public function test_diagnose() {
$DB = $this->tdb;
$result = $DB->diagnose();
$this->assertNull($result, 'Database self diagnostics failed %s');
}
- function test_get_server_info() {
+ public function test_get_server_info() {
$DB = $this->tdb;
$result = $DB->get_server_info();
- $this->assertTrue(is_array($result));
- $this->assertTrue(array_key_exists('description', $result));
- $this->assertTrue(array_key_exists('version', $result));
+ $this->assertInternalType('array', $result);
+ $this->assertArrayHasKey('description', $result);
+ $this->assertArrayHasKey('version', $result);
}
public function test_get_in_or_equal() {
$DB = $this->tdb;
- // SQL_PARAMS_QM - IN or =
+ // SQL_PARAMS_QM - IN or =.
- // Correct usage of multiple values
+ // Correct usage of multiple values.
$in_values = array('value1', 'value2', '3', 4, null, false, true);
list($usql, $params) = $DB->get_in_or_equal($in_values);
- $this->assertEquals('IN ('.implode(',',array_fill(0, count($in_values), '?')).')', $usql);
+ $this->assertSame('IN ('.implode(',', array_fill(0, count($in_values), '?')).')', $usql);
$this->assertEquals(count($in_values), count($params));
foreach ($params as $key => $value) {
$this->assertSame($in_values[$key], $value);
}
- // Correct usage of single value (in an array)
+ // Correct usage of single value (in an array).
$in_values = array('value1');
list($usql, $params) = $DB->get_in_or_equal($in_values);
$this->assertEquals("= ?", $usql);
- $this->assertEquals(1, count($params));
+ $this->assertCount(1, $params);
$this->assertEquals($in_values[0], $params[0]);
- // Correct usage of single value
+ // Correct usage of single value.
$in_value = 'value1';
list($usql, $params) = $DB->get_in_or_equal($in_values);
$this->assertEquals("= ?", $usql);
- $this->assertEquals(1, count($params));
+ $this->assertCount(1, $params);
$this->assertEquals($in_value, $params[0]);
- // SQL_PARAMS_QM - NOT IN or <>
+ // SQL_PARAMS_QM - NOT IN or <>.
- // Correct usage of multiple values
+ // Correct usage of multiple values.
$in_values = array('value1', 'value2', 'value3', 'value4');
list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_QM, null, false);
$this->assertEquals("NOT IN (?,?,?,?)", $usql);
- $this->assertEquals(4, count($params));
+ $this->assertCount(4, $params);
foreach ($params as $key => $value) {
$this->assertEquals($in_values[$key], $value);
}
- // Correct usage of single value (in array()
+ // Correct usage of single value (in array().
$in_values = array('value1');
list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_QM, null, false);
$this->assertEquals("<> ?", $usql);
- $this->assertEquals(1, count($params));
+ $this->assertCount(1, $params);
$this->assertEquals($in_values[0], $params[0]);
- // Correct usage of single value
+ // Correct usage of single value.
$in_value = 'value1';
list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_QM, null, false);
$this->assertEquals("<> ?", $usql);
- $this->assertEquals(1, count($params));
+ $this->assertCount(1, $params);
$this->assertEquals($in_value, $params[0]);
- // SQL_PARAMS_NAMED - IN or =
+ // SQL_PARAMS_NAMED - IN or =.
- // Correct usage of multiple values
+ // Correct usage of multiple values.
$in_values = array('value1', 'value2', 'value3', 'value4');
list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param', true);
- $this->assertEquals(4, count($params));
+ $this->assertCount(4, $params);
reset($in_values);
$ps = array();
foreach ($params as $key => $value) {
}
$this->assertEquals("IN (".implode(',', $ps).")", $usql);
- // Correct usage of single values (in array)
+ // Correct usage of single values (in array).
$in_values = array('value1');
list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param', true);
- $this->assertEquals(1, count($params));
+ $this->assertCount(1, $params);
$value = reset($params);
$key = key($params);
$this->assertEquals("= :$key", $usql);
$this->assertEquals($in_value, $value);
- // Correct usage of single value
+ // Correct usage of single value.
$in_value = 'value1';
list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param', true);
- $this->assertEquals(1, count($params));
+ $this->assertCount(1, $params);
$value = reset($params);
$key = key($params);
$this->assertEquals("= :$key", $usql);
$this->assertEquals($in_value, $value);
- // SQL_PARAMS_NAMED - NOT IN or <>
+ // SQL_PARAMS_NAMED - NOT IN or <>.
- // Correct usage of multiple values
+ // Correct usage of multiple values.
$in_values = array('value1', 'value2', 'value3', 'value4');
list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param', false);
- $this->assertEquals(4, count($params));
+ $this->assertCount(4, $params);
reset($in_values);
$ps = array();
foreach ($params as $key => $value) {
}
$this->assertEquals("NOT IN (".implode(',', $ps).")", $usql);
- // Correct usage of single values (in array)
+ // Correct usage of single values (in array).
$in_values = array('value1');
list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param', false);
- $this->assertEquals(1, count($params));
+ $this->assertCount(1, $params);
$value = reset($params);
$key = key($params);
$this->assertEquals("<> :$key", $usql);
$this->assertEquals($in_value, $value);
- // Correct usage of single value
+ // Correct usage of single value.
$in_value = 'value1';
list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param', false);
- $this->assertEquals(1, count($params));
+ $this->assertCount(1, $params);
$value = reset($params);
$key = key($params);
$this->assertEquals("<> :$key", $usql);
$this->assertEquals($in_value, $value);
- // make sure the param names are unique
- list($usql1, $params1) = $DB->get_in_or_equal(array(1,2,3), SQL_PARAMS_NAMED, 'param');
- list($usql2, $params2) = $DB->get_in_or_equal(array(1,2,3), SQL_PARAMS_NAMED, 'param');
+ // Make sure the param names are unique.
+ list($usql1, $params1) = $DB->get_in_or_equal(array(1, 2, 3), SQL_PARAMS_NAMED, 'param');
+ list($usql2, $params2) = $DB->get_in_or_equal(array(1, 2, 3), SQL_PARAMS_NAMED, 'param');
$params1 = array_keys($params1);
$params2 = array_keys($params2);
$common = array_intersect($params1, $params2);
- $this->assertEquals(count($common), 0);
+ $this->assertCount(0, $common);
- // Some incorrect tests
+ // Some incorrect tests.
- // Incorrect usage passing not-allowed params type
+ // Incorrect usage passing not-allowed params type.
$in_values = array(1, 2, 3);
try {
list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_DOLLAR, 'param', false);
$this->fail('An Exception is missing, expected due to not supported SQL_PARAMS_DOLLAR');
- } catch (exception $e) {
- $this->assertTrue($e instanceof dml_exception);
- $this->assertEquals($e->errorcode, 'typenotimplement');
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('dml_exception', $e);
+ $this->assertSame('typenotimplement', $e->errorcode);
}
- // Incorrect usage passing empty array
+ // Incorrect usage passing empty array.
$in_values = array();
try {
list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param', false);
$this->fail('An Exception is missing, expected due to empty array of items');
- } catch (exception $e) {
- $this->assertTrue($e instanceof coding_exception);
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('coding_exception', $e);
}
- // Test using $onemptyitems
+ // Test using $onemptyitems.
- // Correct usage passing empty array and $onemptyitems = NULL (equal = true, QM)
+ // Correct usage passing empty array and $onemptyitems = null (equal = true, QM).
$in_values = array();
- list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_QM, 'param', true, NULL);
- $this->assertEquals(' IS NULL', $usql);
+ list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_QM, 'param', true, null);
+ $this->assertSame(' IS NULL', $usql);
$this->assertSame(array(), $params);
- // Correct usage passing empty array and $onemptyitems = NULL (equal = false, NAMED)
+ // Correct usage passing empty array and $onemptyitems = null (equal = false, NAMED).
$in_values = array();
- list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param', false, NULL);
- $this->assertEquals(' IS NOT NULL', $usql);
+ list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param', false, null);
+ $this->assertSame(' IS NOT NULL', $usql);
$this->assertSame(array(), $params);
- // Correct usage passing empty array and $onemptyitems = true (equal = true, QM)
+ // Correct usage passing empty array and $onemptyitems = true (equal = true, QM).
$in_values = array();
list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_QM, 'param', true, true);
- $this->assertEquals('= ?', $usql);
+ $this->assertSame('= ?', $usql);
$this->assertSame(array(true), $params);
- // Correct usage passing empty array and $onemptyitems = true (equal = false, NAMED)
+ // Correct usage passing empty array and $onemptyitems = true (equal = false, NAMED).
$in_values = array();
list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param', false, true);
- $this->assertEquals(1, count($params));
+ $this->assertCount(1, $params);
$value = reset($params);
$key = key($params);
- $this->assertEquals('<> :'.$key, $usql);
+ $this->assertSame('<> :'.$key, $usql);
$this->assertSame($value, true);
- // Correct usage passing empty array and $onemptyitems = -1 (equal = true, QM)
+ // Correct usage passing empty array and $onemptyitems = -1 (equal = true, QM).
$in_values = array();
list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_QM, 'param', true, -1);
- $this->assertEquals('= ?', $usql);
+ $this->assertSame('= ?', $usql);
$this->assertSame(array(-1), $params);
- // Correct usage passing empty array and $onemptyitems = -1 (equal = false, NAMED)
+ // Correct usage passing empty array and $onemptyitems = -1 (equal = false, NAMED).
$in_values = array();
list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param', false, -1);
- $this->assertEquals(1, count($params));
+ $this->assertCount(1, $params);
$value = reset($params);
$key = key($params);
- $this->assertEquals('<> :'.$key, $usql);
+ $this->assertSame('<> :'.$key, $usql);
$this->assertSame($value, -1);
- // Correct usage passing empty array and $onemptyitems = 'onevalue' (equal = true, QM)
+ // Correct usage passing empty array and $onemptyitems = 'onevalue' (equal = true, QM).
$in_values = array();
list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_QM, 'param', true, 'onevalue');
- $this->assertEquals('= ?', $usql);
+ $this->assertSame('= ?', $usql);
$this->assertSame(array('onevalue'), $params);
- // Correct usage passing empty array and $onemptyitems = 'onevalue' (equal = false, NAMED)
+ // Correct usage passing empty array and $onemptyitems = 'onevalue' (equal = false, NAMED).
$in_values = array();
list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param', false, 'onevalue');
- $this->assertEquals(1, count($params));
+ $this->assertCount(1, $params);
$value = reset($params);
$key = key($params);
- $this->assertEquals('<> :'.$key, $usql);
+ $this->assertSame('<> :'.$key, $usql);
$this->assertSame($value, 'onevalue');
}
$DB = new moodle_database_for_testing();
$prefix = $DB->get_prefix();
- // Simple placeholder
+ // Simple placeholder.
$placeholder = "{user_123}";
$this->assertSame($prefix."user_123", $DB->public_fix_table_names($placeholder));
- // wrong table name
+ // Wrong table name.
$placeholder = "{user-a}";
$this->assertSame($placeholder, $DB->public_fix_table_names($placeholder));
- // wrong table name
+ // Wrong table name.
$placeholder = "{123user}";
$this->assertSame($placeholder, $DB->public_fix_table_names($placeholder));
- // Full SQL
+ // Full SQL.
$sql = "SELECT * FROM {user}, {funny_table_name}, {mdl_stupid_table} WHERE {user}.id = {funny_table_name}.userid";
$expected = "SELECT * FROM {$prefix}user, {$prefix}funny_table_name, {$prefix}mdl_stupid_table WHERE {$prefix}user.id = {$prefix}funny_table_name.userid";
$this->assertSame($expected, $DB->public_fix_table_names($sql));
}
- function test_fix_sql_params() {
+ public function test_fix_sql_params() {
$DB = $this->tdb;
$prefix = $DB->get_prefix();
$table = $this->get_test_table();
$tablename = $table->getName();
- // Correct table placeholder substitution
+ // Correct table placeholder substitution.
$sql = "SELECT * FROM {{$tablename}}";
$sqlarray = $DB->fix_sql_params($sql);
$this->assertEquals("SELECT * FROM {$prefix}".$tablename, $sqlarray[0]);
- // Conversions of all param types
+ // Conversions of all param types.
$sql = array();
$sql[SQL_PARAMS_NAMED] = "SELECT * FROM {$prefix}testtable WHERE name = :param1, course = :param2";
$sql[SQL_PARAMS_QM] = "SELECT * FROM {$prefix}testtable WHERE name = ?, course = ?";
$this->assertSame($rsql, $sql[$rtype]);
$this->assertSame($rparams, $params[$rtype]);
-
- // Malformed table placeholder
+ // Malformed table placeholder.
$sql = "SELECT * FROM [testtable]";
$sqlarray = $DB->fix_sql_params($sql);
$this->assertSame($sql, $sqlarray[0]);
-
- // Mixed param types (colon and dollar)
+ // Mixed param types (colon and dollar).
$sql = "SELECT * FROM {{$tablename}} WHERE name = :param1, course = \$1";
$params = array('param1' => 'record1', 'param2' => 3);
try {
$DB->fix_sql_params($sql, $params);
$this->fail("Expecting an exception, none occurred");
- } catch (Exception $e) {
- $this->assertTrue($e instanceof dml_exception);
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('dml_exception', $e);
}
- // Mixed param types (question and dollar)
+ // Mixed param types (question and dollar).
$sql = "SELECT * FROM {{$tablename}} WHERE name = ?, course = \$1";
$params = array('param1' => 'record2', 'param2' => 5);
try {
$DB->fix_sql_params($sql, $params);
$this->fail("Expecting an exception, none occurred");
- } catch (Exception $e) {
- $this->assertTrue($e instanceof dml_exception);
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('dml_exception', $e);
}
- // Too few params in sql
+ // Too few params in sql.
$sql = "SELECT * FROM {{$tablename}} WHERE name = ?, course = ?, id = ?";
$params = array('record2', 3);
try {
$DB->fix_sql_params($sql, $params);
$this->fail("Expecting an exception, none occurred");
- } catch (Exception $e) {
- $this->assertTrue($e instanceof dml_exception);
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('dml_exception', $e);
}
- // Too many params in array: no error, just use what is necessary
+ // Too many params in array: no error, just use what is necessary.
$params[] = 1;
$params[] = time();
- try {
- $sqlarray = $DB->fix_sql_params($sql, $params);
- $this->assertTrue(is_array($sqlarray));
- $this->assertEquals(count($sqlarray[1]), 3);
- } catch (Exception $e) {
- $this->fail("Unexpected ".get_class($e)." exception");
- }
+ $sqlarray = $DB->fix_sql_params($sql, $params);
+ $this->assertInternalType('array', $sqlarray);
+ $this->assertCount(3, $sqlarray[1]);
- // Named params missing from array
+ // Named params missing from array.
$sql = "SELECT * FROM {{$tablename}} WHERE name = :name, course = :course";
$params = array('wrongname' => 'record1', 'course' => 1);
try {
$DB->fix_sql_params($sql, $params);
$this->fail("Expecting an exception, none occurred");
- } catch (Exception $e) {
- $this->assertTrue($e instanceof dml_exception);
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('dml_exception', $e);
}
// Duplicate named param in query - this is a very important feature!!
- // it helps with debugging of sloppy code
+ // it helps with debugging of sloppy code.
$sql = "SELECT * FROM {{$tablename}} WHERE name = :name, course = :name";
$params = array('name' => 'record2', 'course' => 3);
try {
$DB->fix_sql_params($sql, $params);
$this->fail("Expecting an exception, none occurred");
- } catch (Exception $e) {
- $this->assertTrue($e instanceof dml_exception);
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('dml_exception', $e);
}
- // Extra named param is ignored
+ // Extra named param is ignored.
$sql = "SELECT * FROM {{$tablename}} WHERE name = :name, course = :course";
$params = array('name' => 'record1', 'course' => 1, 'extrastuff'=>'haha');
- try {
- $sqlarray = $DB->fix_sql_params($sql, $params);
- $this->assertTrue(is_array($sqlarray));
- $this->assertEquals(count($sqlarray[1]), 2);
- } catch (Exception $e) {
- $this->fail("Unexpected ".get_class($e)." exception");
- }
+ $sqlarray = $DB->fix_sql_params($sql, $params);
+ $this->assertInternalType('array', $sqlarray);
+ $this->assertCount(2, $sqlarray[1]);
- // Params exceeding 30 chars length
+ // Params exceeding 30 chars length.
$sql = "SELECT * FROM {{$tablename}} WHERE name = :long_placeholder_with_more_than_30";
$params = array('long_placeholder_with_more_than_30' => 'record1');
try {
$DB->fix_sql_params($sql, $params);
$this->fail("Expecting an exception, none occurred");
- } catch (Exception $e) {
- $this->assertTrue($e instanceof coding_exception);
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('coding_exception', $e);
}
- // Booleans in NAMED params are casting to 1/0 int
+ // Booleans in NAMED params are casting to 1/0 int.
$sql = "SELECT * FROM {{$tablename}} WHERE course = ? OR course = ?";
$params = array(true, false);
list($sql, $params) = $DB->fix_sql_params($sql, $params);
$this->assertTrue(reset($params) === 1);
$this->assertTrue(next($params) === 0);
- // Booleans in QM params are casting to 1/0 int
+ // Booleans in QM params are casting to 1/0 int.
$sql = "SELECT * FROM {{$tablename}} WHERE course = :course1 OR course = :course2";
$params = array('course1' => true, 'course2' => false);
list($sql, $params) = $DB->fix_sql_params($sql, $params);
$this->assertTrue(reset($params) === 1);
$this->assertTrue(next($params) === 0);
- // Booleans in DOLLAR params are casting to 1/0 int
+ // Booleans in DOLLAR params are casting to 1/0 int.
$sql = "SELECT * FROM {{$tablename}} WHERE course = \$1 OR course = \$2";
$params = array(true, false);
list($sql, $params) = $DB->fix_sql_params($sql, $params);
$this->assertTrue(reset($params) === 1);
$this->assertTrue(next($params) === 0);
- // No data types are touched except bool
+ // No data types are touched except bool.
$sql = "SELECT * FROM {{$tablename}} WHERE name IN (?,?,?,?,?,?)";
- $inparams = array('abc', 'ABC', NULL, '1', 1, 1.4);
+ $inparams = array('abc', 'ABC', null, '1', 1, 1.4);
list($sql, $params) = $DB->fix_sql_params($sql, $inparams);
$this->assertSame(array_values($params), array_values($inparams));
}
public function test_strtok() {
- // strtok was previously used by bound emulation, make sure it is not used any more
+ // Strtok was previously used by bound emulation, make sure it is not used any more.
$DB = $this->tdb;
$dbman = $this->tdb->get_manager();
$table = $this->get_test_table();
$tablename = $table->getName();
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, 'lala');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$dbman->create_table($table);
public function test_tweak_param_names() {
// Note the tweak_param_names() method is only available in the oracle driver,
- // hence we look for expected results indirectly, by testing various DML methods
+ // hence we look for expected results indirectly, by testing various DML methods.
// with some "extreme" conditions causing the tweak to happen.
$DB = $this->tdb;
$dbman = $this->tdb->get_manager();
$table = $this->get_test_table();
$tablename = $table->getName();
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- // Add some columns with 28 chars in the name
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ // Add some columns with 28 chars in the name.
$table->add_field('long_int_columnname_with_28c', XMLDB_TYPE_INTEGER, '10');
$table->add_field('long_dec_columnname_with_28c', XMLDB_TYPE_NUMBER, '10,2');
$table->add_field('long_str_columnname_with_28c', XMLDB_TYPE_CHAR, '100');
- // Add some columns with 30 chars in the name
+ // Add some columns with 30 chars in the name.
$table->add_field('long_int_columnname_with_30cxx', XMLDB_TYPE_INTEGER, '10');
$table->add_field('long_dec_columnname_with_30cxx', XMLDB_TYPE_NUMBER, '10,2');
$table->add_field('long_str_columnname_with_30cxx', XMLDB_TYPE_CHAR, '100');
$this->assertTrue($dbman->table_exists($tablename));
- // Test insert record
+ // Test insert record.
$rec1 = new stdClass();
$rec1->long_int_columnname_with_28c = 28;
$rec1->long_dec_columnname_with_28c = 28.28;
$rec1->long_dec_columnname_with_30cxx = 30.30;
$rec1->long_str_columnname_with_30cxx = '30';
- // insert_record()
+ // Insert_record().
$rec1->id = $DB->insert_record($tablename, $rec1);
$this->assertEquals($rec1, $DB->get_record($tablename, array('id' => $rec1->id)));
- // update_record()
+ // Update_record().
$DB->update_record($tablename, $rec1);
$this->assertEquals($rec1, $DB->get_record($tablename, array('id' => $rec1->id)));
- // set_field()
+ // Set_field().
$rec1->long_int_columnname_with_28c = 280;
$DB->set_field($tablename, 'long_int_columnname_with_28c', $rec1->long_int_columnname_with_28c,
array('id' => $rec1->id, 'long_int_columnname_with_28c' => 28));
array('id' => $rec1->id, 'long_str_columnname_with_30cxx' => '30'));
$this->assertEquals($rec1, $DB->get_record($tablename, array('id' => $rec1->id)));
- // delete_records()
+ // Delete_records().
$rec2 = $DB->get_record($tablename, array('id' => $rec1->id));
$rec2->id = $DB->insert_record($tablename, $rec2);
$this->assertEquals(2, $DB->count_records($tablename));
$DB->delete_records($tablename, (array) $rec2);
$this->assertEquals(1, $DB->count_records($tablename));
- // get_recordset()
+ // Get_recordset().
$rs = $DB->get_recordset($tablename, (array) $rec1);
$iterations = 0;
foreach ($rs as $rec2) {
$this->assertEquals(1, $iterations);
$this->assertEquals($rec1, $rec2);
- // get_records()
+ // Get_records().
$recs = $DB->get_records($tablename, (array) $rec1);
- $this->assertEquals(1, count($recs));
+ $this->assertCount(1, $recs);
$this->assertEquals($rec1, reset($recs));
- // get_fieldset_select()
+ // Get_fieldset_select().
$select = 'id = :id AND
long_int_columnname_with_28c = :long_int_columnname_with_28c AND
long_dec_columnname_with_28c = :long_dec_columnname_with_28c AND
long_dec_columnname_with_30cxx = :long_dec_columnname_with_30cxx AND
long_str_columnname_with_30cxx = :long_str_columnname_with_30cxx';
$fields = $DB->get_fieldset_select($tablename, 'long_int_columnname_with_28c', $select, (array)$rec1);
- $this->assertEquals(1, count($fields));
+ $this->assertCount(1, $fields);
$this->assertEquals($rec1->long_int_columnname_with_28c, reset($fields));
$fields = $DB->get_fieldset_select($tablename, 'long_dec_columnname_with_28c', $select, (array)$rec1);
$this->assertEquals($rec1->long_dec_columnname_with_28c, reset($fields));
$fields = $DB->get_fieldset_select($tablename, 'long_str_columnname_with_30cxx', $select, (array)$rec1);
$this->assertEquals($rec1->long_str_columnname_with_30cxx, reset($fields));
- // overlapping placeholders (progressive str_replace)
+ // Overlapping placeholders (progressive str_replace).
$overlapselect = 'id = :p AND
long_int_columnname_with_28c = :param1 AND
long_dec_columnname_with_28c = :param2 AND
'param_' => $rec1->long_dec_columnname_with_30cxx,
'param__' => $rec1->long_str_columnname_with_30cxx);
$recs = $DB->get_records_select($tablename, $overlapselect, $overlapparams);
- $this->assertEquals(1, count($recs));
+ $this->assertCount(1, $recs);
$this->assertEquals($rec1, reset($recs));
- // execute()
+ // Execute().
$DB->execute("DELETE FROM {{$tablename}} WHERE $select", (array)$rec1);
$this->assertEquals(0, $DB->count_records($tablename));
}
$DB = $this->tdb;
$dbman = $this->tdb->get_manager();
- // Need to test with multiple DBs
+ // Need to test with multiple DBs.
$table = $this->get_test_table();
$tablename = $table->getName();
$original_count = count($DB->get_tables());
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$dbman->create_table($table);
$table = $this->get_test_table();
$tablename = $table->getName();
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
$table->add_index('course-id', XMLDB_INDEX_UNIQUE, array('course', 'id'));
$dbman->create_table($table);
$indices = $DB->get_indexes($tablename);
- $this->assertTrue(is_array($indices));
- $this->assertEquals(count($indices), 2);
- // we do not care about index names for now
+ $this->assertInternalType('array', $indices);
+ $this->assertCount(2, $indices);
+ // We do not care about index names for now.
$first = array_shift($indices);
$second = array_shift($indices);
if (count($first['columns']) == 2) {
}
$this->assertFalse($single['unique']);
$this->assertTrue($composed['unique']);
- $this->assertEquals(1, count($single['columns']));
- $this->assertEquals(2, count($composed['columns']));
- $this->assertEquals('course', $single['columns'][0]);
- $this->assertEquals('course', $composed['columns'][0]);
- $this->assertEquals('id', $composed['columns'][1]);
+ $this->assertCount(1, $single['columns']);
+ $this->assertCount(2, $composed['columns']);
+ $this->assertSame('course', $single['columns'][0]);
+ $this->assertSame('course', $composed['columns'][0]);
+ $this->assertSame('id', $composed['columns'][1]);
}
public function test_get_columns() {
$table = $this->get_test_table();
$tablename = $table->getName();
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, 'lala');
$table->add_field('description', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
$table->add_field('enumfield', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, 'test2');
$dbman->create_table($table);
$columns = $DB->get_columns($tablename);
- $this->assertTrue(is_array($columns));
+ $this->assertInternalType('array', $columns);
$fields = $table->getFields();
- $this->assertEquals(count($columns), count($fields));
+ $this->assertCount(count($columns), $fields);
$field = $columns['id'];
- $this->assertEquals('R', $field->meta_type);
+ $this->assertSame('R', $field->meta_type);
$this->assertTrue($field->auto_increment);
$this->assertTrue($field->unique);
$field = $columns['course'];
- $this->assertEquals('I', $field->meta_type);
+ $this->assertSame('I', $field->meta_type);
$this->assertFalse($field->auto_increment);
$this->assertTrue($field->has_default);
$this->assertEquals(0, $field->default_value);
$this->assertTrue($field->not_null);
- for($i=1;$i<=10;$i++) {
+ for ($i=1; $i<=10; $i++) {
$field = $columns['someint'.$i];
- $this->assertEquals('I', $field->meta_type);
+ $this->assertSame('I', $field->meta_type);
$this->assertGreaterThanOrEqual($i, $field->max_length);
}
$field = $columns['someint18'];
- $this->assertEquals('I', $field->meta_type);
+ $this->assertSame('I', $field->meta_type);
$this->assertGreaterThanOrEqual(18, $field->max_length);
$field = $columns['name'];
- $this->assertEquals('C', $field->meta_type);
+ $this->assertSame('C', $field->meta_type);
$this->assertFalse($field->auto_increment);
$this->assertEquals(255, $field->max_length);
$this->assertTrue($field->has_default);
$this->assertFalse($field->not_null);
$field = $columns['description'];
- $this->assertEquals('X', $field->meta_type);
+ $this->assertSame('X', $field->meta_type);
$this->assertFalse($field->auto_increment);
$this->assertFalse($field->has_default);
- $this->assertSame(null, $field->default_value);
+ $this->assertNull($field->default_value);
$this->assertFalse($field->not_null);
$field = $columns['enumfield'];
- $this->assertEquals('C', $field->meta_type);
+ $this->assertSame('C', $field->meta_type);
$this->assertFalse($field->auto_increment);
$this->assertSame('test2', $field->default_value);
$this->assertTrue($field->not_null);
$field = $columns['onenum'];
- $this->assertEquals('N', $field->meta_type);
+ $this->assertSame('N', $field->meta_type);
$this->assertFalse($field->auto_increment);
$this->assertEquals(10, $field->max_length);
$this->assertEquals(2, $field->scale);
$this->assertFalse($field->not_null);
$field = $columns['onefloat'];
- $this->assertEquals('N', $field->meta_type);
+ $this->assertSame('N', $field->meta_type);
$this->assertFalse($field->auto_increment);
$this->assertTrue($field->has_default);
$this->assertEquals(300.0, $field->default_value);
$this->assertFalse($field->not_null);
$field = $columns['anotherfloat'];
- $this->assertEquals('N', $field->meta_type);
+ $this->assertSame('N', $field->meta_type);
$this->assertFalse($field->auto_increment);
$this->assertTrue($field->has_default);
$this->assertEquals(400.0, $field->default_value);
$this->assertFalse($field->not_null);
- // Test negative defaults in numerical columns
+ // Test negative defaults in numerical columns.
$field = $columns['negativedfltint'];
$this->assertTrue($field->has_default);
$this->assertEquals(-1, $field->default_value);
$this->assertEquals($next_column->name, $next_field->getName());
}
- // Test get_columns for non-existing table returns empty array. MDL-30147
+ // Test get_columns for non-existing table returns empty array. MDL-30147.
$columns = $DB->get_columns('xxxx');
$this->assertEquals(array(), $columns);
- // create something similar to "context_temp" with id column without sequence
+ // Create something similar to "context_temp" with id column without sequence.
$dbman->drop_table($table);
$table = $this->get_test_table();
$tablename = $table->getName();
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$dbman->create_table($table);
$DB = $this->tdb;
$dbman = $this->tdb->get_manager();
- $this->assertTrue($dbman instanceof database_manager);
+ $this->assertInstanceOf('database_manager', $dbman);
}
public function test_setup_is_unicodedb() {
$this->assertTrue($DB->setup_is_unicodedb());
}
- public function test_set_debug() { //tests get_debug() too
+ public function test_set_debug() { // Tests get_debug() too.
$DB = $this->tdb;
$dbman = $this->tdb->get_manager();
$table = $this->get_test_table();
$tablename = $table->getName();
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$dbman->create_table($table);
$table1 = $this->get_test_table('1');
$tablename1 = $table1->getName();
- $table1->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table1->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $table1->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table1->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table1->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, '0');
$table1->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
$table1->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table2 = $this->get_test_table('2');
$tablename2 = $table2->getName();
- $table2->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table2->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $table2->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table2->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table2->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$dbman->create_table($table2);
$DB->insert_record($tablename1, array('course' => 7, 'name' => 'ccc'));
$DB->insert_record($tablename1, array('course' => 3, 'name' => 'ddd'));
- // select results are ignored
+ // Select results are ignored.
$sql = "SELECT * FROM {{$tablename1}} WHERE course = :course";
$this->assertTrue($DB->execute($sql, array('course'=>3)));
- // throw exception on error
+ // Throw exception on error.
$sql = "XXUPDATE SET XSSD";
try {
$DB->execute($sql);
$this->fail("Expecting an exception, none occurred");
- } catch (Exception $e) {
- $this->assertTrue($e instanceof dml_exception);
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('dml_exception', $e);
}
- // update records
+ // Update records.
$sql = "UPDATE {{$tablename1}}
SET course = 6
WHERE course = ?";
$this->assertTrue($DB->execute($sql, array('3')));
- $this->assertEquals($DB->count_records($tablename1, array('course' => 6)), 2);
+ $this->assertEquals(2, $DB->count_records($tablename1, array('course' => 6)));
- // update records with subquery condition
- // confirm that the option not using table aliases is cross-db
+ // Update records with subquery condition.
+ // Confirm that the option not using table aliases is cross-db.
$sql = "UPDATE {{$tablename1}}
SET course = 0
WHERE NOT EXISTS (
SELECT course
FROM {{$tablename2}} tbl2
WHERE tbl2.course = {{$tablename1}}.course
- AND 1 = 0)"; // Really we don't update anything, but verify the syntax is allowed
+ AND 1 = 0)"; // Really we don't update anything, but verify the syntax is allowed.
$this->assertTrue($DB->execute($sql));
- // insert from one into second table
+ // Insert from one into second table.
$sql = "INSERT INTO {{$tablename2}} (course)
SELECT course
FROM {{$tablename1}}";
$this->assertTrue($DB->execute($sql));
- $this->assertEquals($DB->count_records($tablename2), 4);
+ $this->assertEquals(4, $DB->count_records($tablename2));
}
public function test_get_recordset() {
$table = $this->get_test_table();
$tablename = $table->getName();
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, '0');
$table->add_field('onetext', XMLDB_TYPE_TEXT, 'big', null, null, null);
$table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
array('course' => 3, 'name' => 'record2', 'onetext'=>'abcd'),
array('course' => 5, 'name' => 'record3', 'onetext'=>'abcde'));
- foreach ($data as $key=>$record) {
+ foreach ($data as $key => $record) {
$data[$key]['id'] = $DB->insert_record($tablename, $record);
}
- // standard recordset iteration
+ // Standard recordset iteration.
$rs = $DB->get_recordset($tablename);
- $this->assertTrue($rs instanceof moodle_recordset);
+ $this->assertInstanceOf('moodle_recordset', $rs);
reset($data);
- foreach($rs as $record) {
+ foreach ($rs as $record) {
$data_record = current($data);
foreach ($record as $k => $v) {
$this->assertEquals($data_record[$k], $v);
}
$rs->close();
- // iterator style usage
+ // Iterator style usage.
$rs = $DB->get_recordset($tablename);
- $this->assertTrue($rs instanceof moodle_recordset);
+ $this->assertInstanceOf('moodle_recordset', $rs);
reset($data);
while ($rs->valid()) {
$record = $rs->current();
}
$rs->close();
- // make sure rewind is ignored
+ // Make sure rewind is ignored.
$rs = $DB->get_recordset($tablename);
- $this->assertTrue($rs instanceof moodle_recordset);
+ $this->assertInstanceOf('moodle_recordset', $rs);
reset($data);
$i = 0;
- foreach($rs as $record) {
+ foreach ($rs as $record) {
$i++;
$rs->rewind();
if ($i > 10) {
}
$rs->close();
- // test for exception throwing on text conditions being compared. (MDL-24863, unwanted auto conversion of param to int)
+ // Test for exception throwing on text conditions being compared. (MDL-24863, unwanted auto conversion of param to int).
$conditions = array('onetext' => '1');
try {
$rs = $DB->get_recordset($tablename, $conditions);
$this->fail('An Exception is missing, expected due to equating of text fields');
- } catch (exception $e) {
- $this->assertTrue($e instanceof dml_exception);
- $this->assertEquals($e->errorcode, 'textconditionsnotallowed');
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('dml_exception', $e);
+ $this->assertSame('textconditionsnotallowed', $e->errorcode);
}
// Test nested iteration.
$rs1 = $DB->get_recordset($tablename);
$i = 0;
- foreach($rs1 as $record1) {
+ foreach ($rs1 as $record1) {
$rs2 = $DB->get_recordset($tablename);
$i++;
$j = 0;
- foreach($rs2 as $record2) {
+ foreach ($rs2 as $record2) {
$j++;
}
$rs2->close();
- $this->assertEquals($j, count($data));
+ $this->assertCount($j, $data);
}
$rs1->close();
- $this->assertEquals($i, count($data));
+ $this->assertCount($i, $data);
- // notes:
+ // Notes:
// * limits are tested in test_get_recordset_sql()
// * where_clause() is used internally and is tested in test_get_records()
}
$table = $this->get_test_table();
$tablename = $table->getName();
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$dbman->create_table($table);
$DB->delete_records($tablename, array('course'=>2));
$i = 0;
- foreach($rs as $record) {
+ foreach ($rs as $record) {
$i++;
$this->assertEquals($i, $record->course);
}
$DB->delete_records($tablename, array('course'=>2));
$i = 0;
- foreach($rs as $record) {
+ foreach ($rs as $record) {
$i++;
$this->assertEquals($i, $record->course);
}
$table = $this->get_test_table();
$tablename = $table->getName();
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, '0');
$table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$data = array(array('course' => 3, 'name' => 'record1'),
array('course' => 3, 'name' => 'record2'),
array('course' => 5, 'name' => 'record3'));
- foreach ($data as $key=>$record) {
+ foreach ($data as $key => $record) {
$data[$key]['id'] = $DB->insert_record($tablename, $record);
}
- // Test repeated numeric keys are returned ok
- $rs = $DB->get_recordset($tablename, NULL, NULL, 'course, name, id');
+ // Test repeated numeric keys are returned ok.
+ $rs = $DB->get_recordset($tablename, null, null, 'course, name, id');
reset($data);
$count = 0;
- foreach($rs as $key => $record) {
+ foreach ($rs as $key => $record) {
$data_record = current($data);
$this->assertEquals($data_record['course'], $key);
next($data);
$count++;
}
$rs->close();
- $this->assertEquals($count, 3);
+ $this->assertEquals(3, $count);
- // Test string keys are returned ok
- $rs = $DB->get_recordset($tablename, NULL, NULL, 'name, course, id');
+ // Test string keys are returned ok.
+ $rs = $DB->get_recordset($tablename, null, null, 'name, course, id');
reset($data);
$count = 0;
- foreach($rs as $key => $record) {
+ foreach ($rs as $key => $record) {
$data_record = current($data);
$this->assertEquals($data_record['name'], $key);
next($data);
$count++;
}
$rs->close();
- $this->assertEquals($count, 3);
+ $this->assertEquals(3, $count);
- // Test numeric not starting in 1 keys are returned ok
- $rs = $DB->get_recordset($tablename, NULL, 'id DESC', 'id, course, name');
+ // Test numeric not starting in 1 keys are returned ok.
+ $rs = $DB->get_recordset($tablename, null, 'id DESC', 'id, course, name');
$data = array_reverse($data);
reset($data);
$count = 0;
- foreach($rs as $key => $record) {
+ foreach ($rs as $key => $record) {
$data_record = current($data);
$this->assertEquals($data_record['id'], $key);
next($data);
$count++;
}
$rs->close();
- $this->assertEquals($count, 3);
+ $this->assertEquals(3, $count);
}
public function test_get_recordset_list() {
$table = $this->get_test_table();
$tablename = $table->getName();
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, null, null, '0');
$table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$dbman->create_table($table);
$this->assertEquals(1, $counter);
$rs->close();
- $rs = $DB->get_recordset_list($tablename, 'course',array()); // Must return 0 rows without conditions. MDL-17645
+ $rs = $DB->get_recordset_list($tablename, 'course', array()); // Must return 0 rows without conditions. MDL-17645.
$counter = 0;
foreach ($rs as $record) {
$rs->close();
$this->assertEquals(0, $counter);
- // notes:
+ // Notes:
// * limits are tested in test_get_recordset_sql()
// * where_clause() is used internally and is tested in test_get_records()
}
$table = $this->get_test_table();
$tablename = $table->getName();
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$dbman->create_table($table);
$rs->close();
$this->assertEquals(2, $counter);
- // notes:
+ // Notes:
// * limits are tested in test_get_recordset_sql()
}
$table = $this->get_test_table();
$tablename = $table->getName();
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$dbman->create_table($table);
$rs->close();
$this->assertEquals(2, $counter);
- // limits - only need to test this case, the rest have been tested by test_get_records_sql()
- // only limitfrom = skips that number of records
+ // Limits - only need to test this case, the rest have been tested by test_get_records_sql()
+ // only limitfrom = skips that number of records.
$rs = $DB->get_recordset_sql("SELECT * FROM {{$tablename}} ORDER BY id", null, 2, 0);
$records = array();
- foreach($rs as $key => $record) {
+ foreach ($rs as $key => $record) {
$records[$key] = $record;
}
$rs->close();
- $this->assertEquals(5, count($records));
+ $this->assertCount(5, $records);
$this->assertEquals($inskey3, reset($records)->id);
$this->assertEquals($inskey7, end($records)->id);
- // note: fetching nulls, empties, LOBs already tested by test_insert_record() no needed here
+ // Note: fetching nulls, empties, LOBs already tested by test_insert_record() no needed here.
}
public function test_export_table_recordset() {
$table = $this->get_test_table();
$tablename = $table->getName();
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('onetext', XMLDB_TYPE_TEXT, 'big', null, null, null);
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$dbman->create_table($table);
$DB->insert_record($tablename, array('course' => 5));
$DB->insert_record($tablename, array('course' => 2));
- // All records
+ // All records.
$records = $DB->get_records($tablename);
- $this->assertEquals(4, count($records));
+ $this->assertCount(4, $records);
$this->assertEquals(3, $records[1]->course);
$this->assertEquals(3, $records[2]->course);
$this->assertEquals(5, $records[3]->course);
$this->assertEquals(2, $records[4]->course);
- // Records matching certain conditions
+ // Records matching certain conditions.
$records = $DB->get_records($tablename, array('course' => 3));
- $this->assertEquals(2, count($records));
+ $this->assertCount(2, $records);
$this->assertEquals(3, $records[1]->course);
$this->assertEquals(3, $records[2]->course);
- // All records sorted by course
+ // All records sorted by course.
$records = $DB->get_records($tablename, null, 'course');
- $this->assertEquals(4, count($records));
+ $this->assertCount(4, $records);
$current_record = reset($records);
$this->assertEquals(4, $current_record->id);
$current_record = next($records);
$current_record = next($records);
$this->assertEquals(3, $current_record->id);
- // All records, but get only one field
+ // All records, but get only one field.
$records = $DB->get_records($tablename, null, '', 'id');
$this->assertFalse(isset($records[1]->course));
$this->assertTrue(isset($records[1]->id));
- $this->assertEquals(4, count($records));
+ $this->assertCount(4, $records);
- // Booleans into params
+ // Booleans into params.
$records = $DB->get_records($tablename, array('course' => true));
- $this->assertEquals(0, count($records));
+ $this->assertCount(0, $records);
$records = $DB->get_records($tablename, array('course' => false));
- $this->assertEquals(0, count($records));
+ $this->assertCount(0, $records);
- // test for exception throwing on text conditions being compared. (MDL-24863, unwanted auto conversion of param to int)
+ // Test for exception throwing on text conditions being compared. (MDL-24863, unwanted auto conversion of param to int).
$conditions = array('onetext' => '1');
try {
$records = $DB->get_records($tablename, $conditions);
if (debugging()) {
- // only in debug mode - hopefully all devs test code in debug mode...
+ // Only in debug mode - hopefully all devs test code in debug mode...
$this->fail('An Exception is missing, expected due to equating of text fields');
}
- } catch (exception $e) {
- $this->assertTrue($e instanceof dml_exception);
- $this->assertEquals($e->errorcode, 'textconditionsnotallowed');
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('dml_exception', $e);
+ $this->assertSame('textconditionsnotallowed', $e->errorcode);
}
- // test get_records passing non-existing table
- // with params
+ // Test get_records passing non-existing table.
+ // with params.
try {
$records = $DB->get_records('xxxx', array('id' => 0));
$this->fail('An Exception is missing, expected due to query against non-existing table');
- } catch (exception $e) {
- $this->assertTrue($e instanceof dml_exception);
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('dml_exception', $e);
if (debugging()) {
- // information for developers only, normal users get general error message
- $this->assertEquals($e->errorcode, 'ddltablenotexist');
+ // Information for developers only, normal users get general error message.
+ $this->assertSame('ddltablenotexist', $e->errorcode);
}
}
- // and without params
+ // And without params.
try {
$records = $DB->get_records('xxxx', array());
$this->fail('An Exception is missing, expected due to query against non-existing table');
- } catch (exception $e) {
- $this->assertTrue($e instanceof dml_exception);
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('dml_exception', $e);
if (debugging()) {
- // information for developers only, normal users get general error message
- $this->assertEquals($e->errorcode, 'ddltablenotexist');
+ // Information for developers only, normal users get general error message.
+ $this->assertSame('ddltablenotexist', $e->errorcode);
}
}
- // test get_records passing non-existing column
+ // Test get_records passing non-existing column.
try {
$records = $DB->get_records($tablename, array('xxxx' => 0));
$this->fail('An Exception is missing, expected due to query against non-existing column');
- } catch (exception $e) {
- $this->assertTrue($e instanceof dml_exception);
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('dml_exception', $e);
if (debugging()) {
- // information for developers only, normal users get general error message
- $this->assertEquals($e->errorcode, 'ddlfieldnotexist');
+ // Information for developers only, normal users get general error message.
+ $this->assertSame('ddlfieldnotexist', $e->errorcode);
}
}
- // note: delegate limits testing to test_get_records_sql()
+ // Note: delegate limits testing to test_get_records_sql().
}
public function test_get_records_list() {
$table = $this->get_test_table();
$tablename = $table->getName();
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$dbman->create_table($table);
$DB->insert_record($tablename, array('course' => 2));
$records = $DB->get_records_list($tablename, 'course', array(3, 2));
- $this->assertTrue(is_array($records));
- $this->assertEquals(3, count($records));
+ $this->assertInternalType('array', $records);
+ $this->assertCount(3, $records);
$this->assertEquals(1, reset($records)->id);
$this->assertEquals(2, next($records)->id);
$this->assertEquals(4, next($records)->id);
- $this->assertSame(array(), $records = $DB->get_records_list($tablename, 'course', array())); // Must return 0 rows without conditions. MDL-17645
- $this->assertEquals(0, count($records));
+ $this->assertSame(array(), $records = $DB->get_records_list($tablename, 'course', array())); // Must return 0 rows without conditions. MDL-17645.
+ $this->assertCount(0, $records);
- // note: delegate limits testing to test_get_records_sql()
+ // Note: delegate limits testing to test_get_records_sql().
}
public function test_get_records_sql() {
$table = $this->get_test_table();
$tablename = $table->getName();
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$dbman->create_table($table);
$table2 = $this->get_test_table("2");
$tablename2 = $table2->getName();
- $table2->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table2->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $table2->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table2->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table2->add_field('nametext', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
$table2->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$dbman->create_table($table2);
$DB->insert_record($tablename2, array('course'=>6, 'nametext'=>'badabong'));
$records = $DB->get_records_sql("SELECT * FROM {{$tablename}} WHERE course = ?", array(3));
- $this->assertEquals(2, count($records));
+ $this->assertCount(2, $records);
$this->assertEquals($inskey1, reset($records)->id);
$this->assertEquals($inskey4, next($records)->id);
- // Awful test, requires debug enabled and sent to browser. Let's do that and restore after test
+ // Awful test, requires debug enabled and sent to browser. Let's do that and restore after test.
$records = $DB->get_records_sql("SELECT course AS id, course AS course FROM {{$tablename}}", null);
$this->assertDebuggingCalled();
- $this->assertEquals(6, count($records));
+ $this->assertCount(6, $records);
set_debugging(DEBUG_MINIMAL);
$records = $DB->get_records_sql("SELECT course AS id, course AS course FROM {{$tablename}}", null);
$this->assertDebuggingNotCalled();
- $this->assertEquals(6, count($records));
+ $this->assertCount(6, $records);
set_debugging(DEBUG_DEVELOPER);
- // negative limits = no limits
+ // Negative limits = no limits.
$records = $DB->get_records_sql("SELECT * FROM {{$tablename}} ORDER BY id", null, -1, -1);
- $this->assertEquals(7, count($records));
+ $this->assertCount(7, $records);
- // zero limits = no limits
+ // Zero limits = no limits.
$records = $DB->get_records_sql("SELECT * FROM {{$tablename}} ORDER BY id", null, 0, 0);
- $this->assertEquals(7, count($records));
+ $this->assertCount(7, $records);
- // only limitfrom = skips that number of records
+ // Only limitfrom = skips that number of records.
$records = $DB->get_records_sql("SELECT * FROM {{$tablename}} ORDER BY id", null, 2, 0);
- $this->assertEquals(5, count($records));
+ $this->assertCount(5, $records);
$this->assertEquals($inskey3, reset($records)->id);
$this->assertEquals($inskey7, end($records)->id);
- // only limitnum = fetches that number of records
+ // Only limitnum = fetches that number of records.
$records = $DB->get_records_sql("SELECT * FROM {{$tablename}} ORDER BY id", null, 0, 3);
- $this->assertEquals(3, count($records));
+ $this->assertCount(3, $records);
$this->assertEquals($inskey1, reset($records)->id);
$this->assertEquals($inskey3, end($records)->id);
- // both limitfrom and limitnum = skips limitfrom records and fetches limitnum ones
+ // Both limitfrom and limitnum = skips limitfrom records and fetches limitnum ones.
$records = $DB->get_records_sql("SELECT * FROM {{$tablename}} ORDER BY id", null, 3, 2);
- $this->assertEquals(2, count($records));
+ $this->assertCount(2, $records);
$this->assertEquals($inskey4, reset($records)->id);
$this->assertEquals($inskey5, end($records)->id);
- // both limitfrom and limitnum in query having subqueris
- // note the subquery skips records with course = 0 and 3
+ // Both limitfrom and limitnum in query having subqueris.
+ // Note the subquery skips records with course = 0 and 3.
$sql = "SELECT * FROM {{$tablename}}
WHERE course NOT IN (
SELECT course FROM {{$tablename}}
WHERE course IN (0, 3))
ORDER BY course";
- $records = $DB->get_records_sql($sql, null, 0, 2); // Skip 0, get 2
- $this->assertEquals(2, count($records));
+ $records = $DB->get_records_sql($sql, null, 0, 2); // Skip 0, get 2.
+ $this->assertCount(2, $records);
$this->assertEquals($inskey6, reset($records)->id);
$this->assertEquals($inskey5, end($records)->id);
- $records = $DB->get_records_sql($sql, null, 2, 2); // Skip 2, get 2
- $this->assertEquals(2, count($records));
+ $records = $DB->get_records_sql($sql, null, 2, 2); // Skip 2, get 2.
+ $this->assertCount(2, $records);
$this->assertEquals($inskey3, reset($records)->id);
$this->assertEquals($inskey2, end($records)->id);
- // test 2 tables with aliases and limits with order bys
+ // Test 2 tables with aliases and limits with order bys.
$sql = "SELECT t1.id, t1.course AS cid, t2.nametext
FROM {{$tablename}} t1, {{$tablename2}} t2
WHERE t2.course=t1.course
ORDER BY t1.course, ". $DB->sql_compare_text('t2.nametext');
- $records = $DB->get_records_sql($sql, null, 2, 2); // Skip courses 3 and 6, get 4 and 5
- $this->assertEquals(2, count($records));
- $this->assertEquals('5', end($records)->cid);
- $this->assertEquals('4', reset($records)->cid);
+ $records = $DB->get_records_sql($sql, null, 2, 2); // Skip courses 3 and 6, get 4 and 5.
+ $this->assertCount(2, $records);
+ $this->assertSame('5', end($records)->cid);
+ $this->assertSame('4', reset($records)->cid);
- // test 2 tables with aliases and limits with the highest INT limit works
- $records = $DB->get_records_sql($sql, null, 2, PHP_INT_MAX); // Skip course {3,6}, get {4,5}
- $this->assertEquals(2, count($records));
- $this->assertEquals('5', end($records)->cid);
- $this->assertEquals('4', reset($records)->cid);
+ // Test 2 tables with aliases and limits with the highest INT limit works.
+ $records = $DB->get_records_sql($sql, null, 2, PHP_INT_MAX); // Skip course {3,6}, get {4,5}.
+ $this->assertCount(2, $records);
+ $this->assertSame('5', end($records)->cid);
+ $this->assertSame('4', reset($records)->cid);
- // test 2 tables with aliases and limits with order bys (limit which is highest INT number)
- $records = $DB->get_records_sql($sql, null, PHP_INT_MAX, 2); // Skip all courses
- $this->assertEquals(0, count($records));
+ // Test 2 tables with aliases and limits with order bys (limit which is highest INT number).
+ $records = $DB->get_records_sql($sql, null, PHP_INT_MAX, 2); // Skip all courses.
+ $this->assertCount(0, $records);
- // test 2 tables with aliases and limits with order bys (limit which s highest INT number)
- $records = $DB->get_records_sql($sql, null, PHP_INT_MAX, PHP_INT_MAX); // Skip all courses
- $this->assertEquals(0, count($records));
+ // Test 2 tables with aliases and limits with order bys (limit which s highest INT number).
+ $records = $DB->get_records_sql($sql, null, PHP_INT_MAX, PHP_INT_MAX); // Skip all courses.
+ $this->assertCount(0, $records);
- // TODO: Test limits in queries having DISTINCT clauses
+ // TODO: Test limits in queries having DISTINCT clauses.
- // note: fetching nulls, empties, LOBs already tested by test_update_record() no needed here
+ // Note: fetching nulls, empties, LOBs already tested by test_update_record() no needed here.
}
public function test_get_records_menu() {
$table = $this->get_test_table();
$tablename = $table->getName();
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$dbman->create_table($table);
$DB->insert_record($tablename, array('course' => 2));
$records = $DB->get_records_menu($tablename, array('course' => 3));
- $this->assertTrue(is_array($records));
- $this->assertEquals(2, count($records));
- $this->assertFalse(empty($records[1]));
- $this->assertFalse(empty($records[2]));
+ $this->assertInternalType('array', $records);
+ $this->assertCount(2, $records);
+ $this->assertNotEmpty($records[1]);
+ $this->assertNotEmpty($records[2]);
$this->assertEquals(3, $records[1]);
$this->assertEquals(3, $records[2]);
- // note: delegate limits testing to test_get_records_sql()
+ // Note: delegate limits testing to test_get_records_sql().
}
public function test_get_records_select_menu() {
$table = $this->get_test_table();
$tablename = $table->getName();
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$dbman->create_table($table);
$DB->insert_record($tablename, array('course' => 5));
$records = $DB->get_records_select_menu($tablename, "course > ?", array(2));
- $this->assertTrue(is_array($records));
-
- $this->assertEquals(3, count($records));
- $this->assertFalse(empty($records[1]));
- $this->assertTrue(empty($records[2]));
- $this->assertFalse(empty($records[3]));
- $this->assertFalse(empty($records[4]));
- $this->assertEquals(3, $records[1]);
- $this->assertEquals(3, $records[3]);
- $this->assertEquals(5, $records[4]);
-
- // note: delegate limits testing to test_get_records_sql()
+ $this->assertInternalType('array', $records);
+
+ $this->assertCount(3, $records);
+ $this->assertArrayHasKey(1, $records);
+ $this->assertArrayNotHasKey(2, $records);
+ $this->assertArrayHasKey(3, $records);
+ $this->assertArrayHasKey(4, $records);
+ $this->assertSame('3', $records[1]);
+ $this->assertSame('3', $records[3]);
+ $this->assertSame('5', $records[4]);
+
+ // Note: delegate limits testing to test_get_records_sql().
}
public function test_get_records_sql_menu() {
$table = $this->get_test_table();
$tablename = $table->getName();
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$dbman->create_table($table);
$DB->insert_record($tablename, array('course' => 5));
$records = $DB->get_records_sql_menu("SELECT * FROM {{$tablename}} WHERE course > ?", array(2));
- $this->assertTrue(is_array($records));
-
- $this->assertEquals(3, count($records));
- $this->assertFalse(empty($records[1]));
- $this->assertTrue(empty($records[2]));
- $this->assertFalse(empty($records[3]));
- $this->assertFalse(empty($records[4]));
- $this->assertEquals(3, $records[1]);
- $this->assertEquals(3, $records[3]);
- $this->assertEquals(5, $records[4]);
-
- // note: delegate limits testing to test_get_records_sql()
+ $this->assertInternalType('array', $records);
+
+ $this->assertCount(3, $records);
+ $this->assertArrayHasKey(1, $records);
+ $this->assertArrayNotHasKey(2, $records);
+ $this->assertArrayHasKey(3, $records);
+ $this->assertArrayHasKey(4, $records);
+ $this->assertSame('3', $records[1]);
+ $this->assertSame('3', $records[3]);
+ $this->assertSame('5', $records[4]);
+
+ // Note: delegate limits testing to test_get_records_sql().
}
public function test_get_record() {
$table = $this->get_test_table();
$tablename = $table->getName();
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$dbman->create_table($table);
$DB->insert_record($tablename, array('course' => 2));
$record = $DB->get_record($tablename, array('id' => 2));
- $this->assertTrue($record instanceof stdClass);
+ $this->assertInstanceOf('stdClass', $record);
$this->assertEquals(2, $record->course);
$this->assertEquals(2, $record->id);
$table = $this->get_test_table();
$tablename = $table->getName();
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$dbman->create_table($table);
$DB->insert_record($tablename, array('course' => 2));
$record = $DB->get_record_select($tablename, "id = ?", array(2));
- $this->assertTrue($record instanceof stdClass);
+ $this->assertInstanceOf('stdClass', $record);
$this->assertEquals(2, $record->course);
- // note: delegates limit testing to test_get_records_sql()
+ // Note: delegates limit testing to test_get_records_sql().
}
public function test_get_record_sql() {
$table = $this->get_test_table();
$tablename = $table->getName();
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$dbman->create_table($table);
$DB->insert_record($tablename, array('course' => 3));
$DB->insert_record($tablename, array('course' => 2));
- // standard use
+ // Standard use.
$record = $DB->get_record_sql("SELECT * FROM {{$tablename}} WHERE id = ?", array(2));
- $this->assertTrue($record instanceof stdClass);
+ $this->assertInstanceOf('stdClass', $record);
$this->assertEquals(2, $record->course);
$this->assertEquals(2, $record->id);
- // backwards compatibility with $ignoremultiple
+ // Backwards compatibility with $ignoremultiple.
$this->assertFalse((bool)IGNORE_MISSING);
$this->assertTrue((bool)IGNORE_MULTIPLE);
- // record not found - ignore
+ // Record not found - ignore.
$this->assertFalse($DB->get_record_sql("SELECT * FROM {{$tablename}} WHERE id = ?", array(666), IGNORE_MISSING));
$this->assertFalse($DB->get_record_sql("SELECT * FROM {{$tablename}} WHERE id = ?", array(666), IGNORE_MULTIPLE));
- // record not found error
+ // Record not found error.
try {
$DB->get_record_sql("SELECT * FROM {{$tablename}} WHERE id = ?", array(666), MUST_EXIST);
$this->fail("Exception expected");
$this->assertDebuggingNotCalled();
set_debugging(DEBUG_DEVELOPER);
- // multiple matches ignored
+ // Multiple matches ignored.
$this->assertNotEmpty($DB->get_record_sql("SELECT * FROM {{$tablename}}", array(), IGNORE_MULTIPLE));
- // multiple found error
+ // Multiple found error.
try {
$DB->get_record_sql("SELECT * FROM {{$tablename}}", array(), MUST_EXIST);
$this->fail("Exception expected");
$table = $this->get_test_table();
$tablename = $table->getName();
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('onetext', XMLDB_TYPE_TEXT, 'big', null, null, null);
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$dbman->create_table($table);
$this->assertEquals(3, $DB->get_field($tablename, 'course', array('id' => $id1)));
$this->assertEquals(3, $DB->get_field($tablename, 'course', array('course' => 3)));
- $this->assertSame(false, $DB->get_field($tablename, 'course', array('course' => 11), IGNORE_MISSING));
+ $this->assertFalse($DB->get_field($tablename, 'course', array('course' => 11), IGNORE_MISSING));
try {
$DB->get_field($tablename, 'course', array('course' => 4), MUST_EXIST);
$this->fail('Exception expected due to missing record');
$this->assertEquals(5, $DB->get_field($tablename, 'course', array('course' => 5), IGNORE_MISSING));
$this->assertDebuggingCalled();
- // test for exception throwing on text conditions being compared. (MDL-24863, unwanted auto conversion of param to int)
+ // Test for exception throwing on text conditions being compared. (MDL-24863, unwanted auto conversion of param to int).
$conditions = array('onetext' => '1');
try {
$DB->get_field($tablename, 'course', $conditions);
if (debugging()) {
- // only in debug mode - hopefully all devs test code in debug mode...
+ // Only in debug mode - hopefully all devs test code in debug mode...
$this->fail('An Exception is missing, expected due to equating of text fields');
}
- } catch (exception $e) {
- $this->assertTrue($e instanceof dml_exception);
- $this->assertEquals($e->errorcode, 'textconditionsnotallowed');
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('dml_exception', $e);
+ $this->assertSame('textconditionsnotallowed', $e->errorcode);
}
}
$table = $this->get_test_table();
$tablename = $table->getName();
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$dbman->create_table($table);
$table = $this->get_test_table();
$tablename = $table->getName();
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$dbman->create_table($table);
$table = $this->get_test_table();
$tablename = $table->getName();
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$dbman->create_table($table);
$DB->insert_record($tablename, array('course' => 6));
$fieldset = $DB->get_fieldset_select($tablename, 'course', "course > ?", array(1));
- $this->assertTrue(is_array($fieldset));
+ $this->assertInternalType('array', $fieldset);
- $this->assertEquals(3, count($fieldset));
+ $this->assertCount(3, $fieldset);
$this->assertEquals(3, $fieldset[0]);
$this->assertEquals(2, $fieldset[1]);
$this->assertEquals(6, $fieldset[2]);
$table = $this->get_test_table();
$tablename = $table->getName();
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$dbman->create_table($table);
$DB->insert_record($tablename, array('course' => 6));
$fieldset = $DB->get_fieldset_sql("SELECT * FROM {{$tablename}} WHERE course > ?", array(1));
- $this->assertTrue(is_array($fieldset));
+ $this->assertInternalType('array', $fieldset);
- $this->assertEquals(3, count($fieldset));
+ $this->assertCount(3, $fieldset);
$this->assertEquals(2, $fieldset[0]);
$this->assertEquals(3, $fieldset[1]);
$this->assertEquals(4, $fieldset[2]);
$table = $this->get_test_table();
$tablename = $table->getName();
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('onechar', XMLDB_TYPE_CHAR, '100', null, null, null, 'onestring');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$dbman->create_table($table);
$this->assertEquals($record, $before);
$record = $DB->get_record($tablename, array('course' => 1));
- $this->assertTrue($record instanceof stdClass);
+ $this->assertInstanceOf('stdClass', $record);
$this->assertSame('xx', $record->onechar);
$result = $DB->insert_record_raw($tablename, array('course' => 2, 'onechar' => 'yy'), false);
- $this->assertSame(true, $result);
+ $this->assertTrue($result);
- // note: bulk not implemented yet
+ // Note: bulk not implemented yet.
$DB->insert_record_raw($tablename, array('course' => 3, 'onechar' => 'zz'), true, true);
$record = $DB->get_record($tablename, array('course' => 3));
- $this->assertTrue($record instanceof stdClass);
+ $this->assertInstanceOf('stdClass', $record);
$this->assertSame('zz', $record->onechar);
- // custom sequence (id) - returnid is ignored
+ // Custom sequence (id) - returnid is ignored.
$result = $DB->insert_record_raw($tablename, array('id' => 10, 'course' => 3, 'onechar' => 'bb'), true, false, true);
- $this->assertSame(true, $result);
+ $this->assertTrue($result);
$record = $DB->get_record($tablename, array('id' => 10));
- $this->assertTrue($record instanceof stdClass);
+ $this->assertInstanceOf('stdClass', $record);
$this->assertSame('bb', $record->onechar);
- // custom sequence - missing id error
+ // Custom sequence - missing id error.
try {
$DB->insert_record_raw($tablename, array('course' => 3, 'onechar' => 'bb'), true, false, true);
$this->fail('Exception expected due to missing record');
$this->assertTrue(true);
}
- // wrong column error
+ // Wrong column error.
try {
$DB->insert_record_raw($tablename, array('xxxxx' => 3, 'onechar' => 'bb'));
$this->fail('Exception expected due to invalid column');
$this->assertTrue(true);
}
- // create something similar to "context_temp" with id column without sequence
+ // Create something similar to "context_temp" with id column without sequence.
$dbman->drop_table($table);
$table = $this->get_test_table();
$tablename = $table->getName();
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$dbman->create_table($table);
$table = $this->get_test_table();
$tablename = $table->getName();
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
- $table->add_field('oneint', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, 100);
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
+ $table->add_field('oneint', XMLDB_TYPE_INTEGER, '10', null, null, null, 100);
$table->add_field('onenum', XMLDB_TYPE_NUMBER, '10,2', null, null, null, 200);
$table->add_field('onechar', XMLDB_TYPE_CHAR, '100', null, null, null, 'onestring');
$table->add_field('onetext', XMLDB_TYPE_TEXT, 'big', null, null, null);
$this->assertSame(1, $DB->insert_record($tablename, array('course' => 1), true));
$record = $DB->get_record($tablename, array('course' => 1));
$this->assertEquals(1, $record->id);
- $this->assertEquals(100, $record->oneint); // Just check column defaults have been applied
+ $this->assertEquals(100, $record->oneint); // Just check column defaults have been applied.
$this->assertEquals(200, $record->onenum);
$this->assertSame('onestring', $record->onechar);
$this->assertNull($record->onetext);
$this->assertNull($record->onebinary);
- // without returning id, bulk not implemented
- $result = $this->assertSame(true, $DB->insert_record($tablename, array('course' => 99), false, true));
+ // Without returning id, bulk not implemented.
+ $result = $this->assertTrue($DB->insert_record($tablename, array('course' => 99), false, true));
$record = $DB->get_record($tablename, array('course' => 99));
$this->assertEquals(2, $record->id);
$this->assertEquals(99, $record->course);
- // Check nulls are set properly for all types
+ // Check nulls are set properly for all types.
$record = new stdClass();
$record->oneint = null;
$record->onenum = null;
$this->assertNull($record->onetext);
$this->assertNull($record->onebinary);
- // Check zeros are set properly for all types
+ // Check zeros are set properly for all types.
$record = new stdClass();
$record->oneint = 0;
$record->onenum = 0;
$this->assertEquals(0, $record->oneint);
$this->assertEquals(0, $record->onenum);
- // Check booleans are set properly for all types
+ // Check booleans are set properly for all types.
$record = new stdClass();
- $record->oneint = true; // trues
+ $record->oneint = true; // Trues.
$record->onenum = true;
$record->onechar = true;
$record->onetext = true;
$this->assertEquals(1, $record->onetext);
$record = new stdClass();
- $record->oneint = false; // falses
+ $record->oneint = false; // Falses.
$record->onenum = false;
$record->onechar = false;
$record->onetext = false;
$this->assertEquals(0, $record->onechar);
$this->assertEquals(0, $record->onetext);
- // Check string data causes exception in numeric types
+ // Check string data causes exception in numeric types.
$record = new stdClass();
$record->oneint = 'onestring';
$record->onenum = 0;
try {
$DB->insert_record($tablename, $record);
$this->fail("Expecting an exception, none occurred");
- } catch (exception $e) {
- $this->assertTrue($e instanceof dml_exception);
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('dml_exception', $e);
}
$record = new stdClass();
$record->oneint = 0;
try {
$DB->insert_record($tablename, $record);
$this->fail("Expecting an exception, none occurred");
- } catch (exception $e) {
- $this->assertTrue($e instanceof dml_exception);
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('dml_exception', $e);
}
- // Check empty string data is stored as 0 in numeric datatypes
+ // Check empty string data is stored as 0 in numeric datatypes.
$record = new stdClass();
- $record->oneint = ''; // empty string
+ $record->oneint = ''; // Empty string.
$record->onenum = 0;
$recid = $DB->insert_record($tablename, $record);
$record = $DB->get_record($tablename, array('id' => $recid));
$record = new stdClass();
$record->oneint = 0;
- $record->onenum = ''; // empty string
+ $record->onenum = ''; // Empty string.
$recid = $DB->insert_record($tablename, $record);
$record = $DB->get_record($tablename, array('id' => $recid));
$this->assertTrue(is_numeric($record->onenum) && $record->onenum == 0);
- // Check empty strings are set properly in string types
+ // Check empty strings are set properly in string types.
$record = new stdClass();
$record->oneint = 0;
$record->onenum = 0;
$this->assertTrue($record->onechar === '');
$this->assertTrue($record->onetext === '');
- // Check operation ((210.10 + 39.92) - 150.02) against numeric types
+ // Check operation ((210.10 + 39.92) - 150.02) against numeric types.
$record = new stdClass();
$record->oneint = ((210.10 + 39.92) - 150.02);
$record->onenum = ((210.10 + 39.92) - 150.02);
$this->assertEquals(100, $record->oneint);
$this->assertEquals(100, $record->onenum);
- // Check various quotes/backslashes combinations in string types
+ // Check various quotes/backslashes combinations in string types.
$teststrings = array(
'backslashes and quotes alone (even): "" \'\' \\\\',
'backslashes and quotes alone (odd): """ \'\'\' \\\\\\',
$this->assertEquals($teststring, $record->onetext);
}
- // Check LOBs in text/binary columns
+ // Check LOBs in text/binary columns.
$clob = file_get_contents(__DIR__ . '/fixtures/clob.txt');
$blob = file_get_contents(__DIR__ . '/fixtures/randombinary');
$record = new stdClass();
$this->assertEquals($clob, $record->onetext, 'Test CLOB insert (full contents output disabled)');
$this->assertEquals($blob, $record->onebinary, 'Test BLOB insert (full contents output disabled)');
- // And "small" LOBs too, just in case
+ // And "small" LOBs too, just in case.
$newclob = substr($clob, 0, 500);
$newblob = substr($blob, 0, 250);
$record = new stdClass();
$rs->close();
$this->assertEquals($newclob, $record->onetext, 'Test "small" CLOB insert (full contents output disabled)');
$this->assertEquals($newblob, $record->onebinary, 'Test "small" BLOB insert (full contents output disabled)');
- $this->assertEquals(false, $rs->key()); // Ensure recordset key() method to be working ok after closing
+ $this->assertEquals(false, $rs->key()); // Ensure recordset key() method to be working ok after closing.
- // And "diagnostic" LOBs too, just in case
+ // And "diagnostic" LOBs too, just in case.
$newclob = '\'"\\;/ěščřžýáíé';
$newblob = '\'"\\;/ěščřžýáíé';
$record = new stdClass();
$rs->close();
$this->assertSame($newclob, $record->onetext);
$this->assertSame($newblob, $record->onebinary);
- $this->assertEquals(false, $rs->key()); // Ensure recordset key() method to be working ok after closing
+ $this->assertEquals(false, $rs->key()); // Ensure recordset key() method to be working ok after closing.
- // test data is not modified
+ // Test data is not modified.
$record = new stdClass();
- $record->id = -1; // has to be ignored
+ $record->id = -1; // Has to be ignored.
$record->course = 3;
- $record->lalala = 'lalal'; // unused
+ $record->lalala = 'lalal'; // Unused.
$before = clone($record);
$DB->insert_record($tablename, $record);
$this->assertEquals($record, $before);
- // make sure the id is always increasing and never reuses the same id
+ // Make sure the id is always increasing and never reuses the same id.
$id1 = $DB->insert_record($tablename, array('course' => 3));
$id2 = $DB->insert_record($tablename, array('course' => 3));
$this->assertTrue($id1 < $id2);
// Let's insert one record violating the constraint multiple times.
$record = (object)array('course' => 1, 'oneint' => 1);
- $this->assertTrue($DB->insert_record($tablename, $record, false)); // insert 1st. No problem expected.
+ $this->assertTrue($DB->insert_record($tablename, $record, false)); // Insert 1st. No problem expected.
// Re-insert same record, not returning id. dml_exception expected.
try {
$DB->insert_record($tablename, $record, false);
$this->fail("Expecting an exception, none occurred");
- } catch (exception $e) {
- $this->assertTrue($e instanceof dml_exception);
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('dml_exception', $e);
}
// Re-insert same record, returning id. dml_exception expected.
try {
$DB->insert_record($tablename, $record, true);
$this->fail("Expecting an exception, none occurred");
- } catch (exception $e) {
- $this->assertTrue($e instanceof dml_exception);
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('dml_exception', $e);
}
}
$table = $this->get_test_table();
$tablename = $table->getName();
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
- $table->add_field('oneint', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, 100);
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
+ $table->add_field('oneint', XMLDB_TYPE_INTEGER, '10', null, null, null, 100);
$table->add_field('onenum', XMLDB_TYPE_NUMBER, '10,2', null, null, null, 200);
$table->add_field('onechar', XMLDB_TYPE_CHAR, '100', null, null, null, 'onestring');
$table->add_field('onetext', XMLDB_TYPE_TEXT, 'big', null, null, null);
$this->assertSame(1, $DB->insert_record($tablename, array('course' => 1), true));
$record = $DB->get_record($tablename, array('course' => 1));
$this->assertEquals(1, $record->id);
- $this->assertEquals(100, $record->oneint); // Just check column defaults have been applied
+ $this->assertEquals(100, $record->oneint); // Just check column defaults have been applied.
$this->assertEquals(200, $record->onenum);
$this->assertSame('onestring', $record->onechar);
$this->assertNull($record->onetext);
$this->assertNull($record->onebinary);
- // ignore extra columns
+ // Ignore extra columns.
$record = (object)array('id'=>13, 'course'=>2, 'xxxx'=>788778);
$before = clone($record);
- $this->assertSame(true, $DB->import_record($tablename, $record));
+ $this->assertTrue($DB->import_record($tablename, $record));
$this->assertEquals($record, $before);
$records = $DB->get_records($tablename);
$this->assertEquals(2, $records[13]->course);
- // Check nulls are set properly for all types
+ // Check nulls are set properly for all types.
$record = new stdClass();
$record->id = 20;
$record->oneint = null;
$this->assertNull($record->onetext);
$this->assertNull($record->onebinary);
- // Check zeros are set properly for all types
+ // Check zeros are set properly for all types.
$record = new stdClass();
$record->id = 23;
$record->oneint = 0;
$this->assertEquals(0, $record->oneint);
$this->assertEquals(0, $record->onenum);
- // Check string data causes exception in numeric types
+ // Check string data causes exception in numeric types.
$record = new stdClass();
$record->id = 32;
$record->oneint = 'onestring';
try {
$DB->import_record($tablename, $record);
$this->fail("Expecting an exception, none occurred");
- } catch (exception $e) {
- $this->assertTrue($e instanceof dml_exception);
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('dml_exception', $e);
}
$record = new stdClass();
$record->id = 35;
try {
$DB->import_record($tablename, $record);
$this->fail("Expecting an exception, none occurred");
- } catch (exception $e) {
- $this->assertTrue($e instanceof dml_exception);
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('dml_exception', $e);
}
- // Check empty strings are set properly in string types
+ // Check empty strings are set properly in string types.
$record = new stdClass();
$record->id = 44;
$record->oneint = 0;
$this->assertTrue($record->onechar === '');
$this->assertTrue($record->onetext === '');
- // Check operation ((210.10 + 39.92) - 150.02) against numeric types
+ // Check operation ((210.10 + 39.92) - 150.02) against numeric types.
$record = new stdClass();
$record->id = 47;
$record->oneint = ((210.10 + 39.92) - 150.02);
$this->assertEquals(100, $record->oneint);
$this->assertEquals(100, $record->onenum);
- // Check various quotes/backslashes combinations in string types
+ // Check various quotes/backslashes combinations in string types.
$i = 50;
$teststrings = array(
'backslashes and quotes alone (even): "" \'\' \\\\',
$i = $i + 3;
}
- // Check LOBs in text/binary columns
+ // Check LOBs in text/binary columns.
$clob = file_get_contents(__DIR__ . '/fixtures/clob.txt');
$record = new stdClass();
$record->id = 70;
$rs->close();
$this->assertEquals($blob, $record->onebinary, 'Test BLOB insert (full contents output disabled)');
- // And "small" LOBs too, just in case
+ // And "small" LOBs too, just in case.
$newclob = substr($clob, 0, 500);
$newblob = substr($blob, 0, 250);
$record = new stdClass();
$rs->close();
$this->assertEquals($newclob, $record->onetext, 'Test "small" CLOB insert (full contents output disabled)');
$this->assertEquals($newblob, $record->onebinary, 'Test "small" BLOB insert (full contents output disabled)');
- $this->assertEquals(false, $rs->key()); // Ensure recordset key() method to be working ok after closing
+ $this->assertEquals(false, $rs->key()); // Ensure recordset key() method to be working ok after closing.
}
public function test_update_record_raw() {
$table = $this->get_test_table();
$tablename = $table->getName();
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$dbman->create_table($table);
try {
$DB->update_record_raw($tablename, $record);
$this->fail("Expecting an exception, none occurred");
- } catch (Exception $e) {
- $this->assertTrue($e instanceof moodle_exception);
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('moodle_exception', $e);
}
$record = $DB->get_record($tablename, array('course' => 3));
try {
$DB->update_record_raw($tablename, $record);
$this->fail("Expecting an exception, none occurred");
- } catch (Exception $e) {
- $this->assertTrue($e instanceof coding_exception);
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('coding_exception', $e);
}
}
$table = $this->get_test_table();
$tablename = $table->getName();
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
- $table->add_field('oneint', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, 100);
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
+ $table->add_field('oneint', XMLDB_TYPE_INTEGER, '10', null, null, null, 100);
$table->add_field('onenum', XMLDB_TYPE_NUMBER, '10,2', null, null, null, 200);
$table->add_field('onechar', XMLDB_TYPE_CHAR, '100', null, null, null, 'onestring');
$table->add_field('onetext', XMLDB_TYPE_TEXT, 'big', null, null, null);
$this->assertTrue($DB->update_record($tablename, $record));
$this->assertFalse($record = $DB->get_record($tablename, array('course' => 1)));
$this->assertNotEmpty($record = $DB->get_record($tablename, array('course' => 2)));
- $this->assertEquals(100, $record->oneint); // Just check column defaults have been applied
+ $this->assertEquals(100, $record->oneint); // Just check column defaults have been applied.
$this->assertEquals(200, $record->onenum);
- $this->assertEquals('onestring', $record->onechar);
+ $this->assertSame('onestring', $record->onechar);
$this->assertNull($record->onetext);
$this->assertNull($record->onebinary);
- // Check nulls are set properly for all types
+ // Check nulls are set properly for all types.
$record->oneint = null;
$record->onenum = null;
$record->onechar = null;
$this->assertNull($record->onetext);
$this->assertNull($record->onebinary);
- // Check zeros are set properly for all types
+ // Check zeros are set properly for all types.
$record->oneint = 0;
$record->onenum = 0;
$DB->update_record($tablename, $record);
$this->assertEquals(0, $record->oneint);
$this->assertEquals(0, $record->onenum);
- // Check booleans are set properly for all types
- $record->oneint = true; // trues
+ // Check booleans are set properly for all types.
+ $record->oneint = true; // Trues.
$record->onenum = true;
$record->onechar = true;
$record->onetext = true;
$this->assertEquals(1, $record->onechar);
$this->assertEquals(1, $record->onetext);
- $record->oneint = false; // falses
+ $record->oneint = false; // Falses.
$record->onenum = false;
$record->onechar = false;
$record->onetext = false;
$this->assertEquals(0, $record->onechar);
$this->assertEquals(0, $record->onetext);
- // Check string data causes exception in numeric types
+ // Check string data causes exception in numeric types.
$record->oneint = 'onestring';
$record->onenum = 0;
try {
$DB->update_record($tablename, $record);
$this->fail("Expecting an exception, none occurred");
- } catch (exception $e) {
- $this->assertTrue($e instanceof dml_exception);
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('dml_exception', $e);
}
$record->oneint = 0;
$record->onenum = 'onestring';
try {
$DB->update_record($tablename, $record);
$this->fail("Expecting an exception, none occurred");
- } catch (exception $e) {
- $this->assertTrue($e instanceof dml_exception);
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('dml_exception', $e);
}
- // Check empty string data is stored as 0 in numeric datatypes
- $record->oneint = ''; // empty string
+ // Check empty string data is stored as 0 in numeric datatypes.
+ $record->oneint = ''; // Empty string.
$record->onenum = 0;
$DB->update_record($tablename, $record);
$record = $DB->get_record($tablename, array('course' => 2));
$this->assertTrue(is_numeric($record->oneint) && $record->oneint == 0);
$record->oneint = 0;
- $record->onenum = ''; // empty string
+ $record->onenum = ''; // Empty string.
$DB->update_record($tablename, $record);
$record = $DB->get_record($tablename, array('course' => 2));
$this->assertTrue(is_numeric($record->onenum) && $record->onenum == 0);
- // Check empty strings are set properly in string types
+ // Check empty strings are set properly in string types.
$record->oneint = 0;
$record->onenum = 0;
$record->onechar = '';
$this->assertTrue($record->onechar === '');
$this->assertTrue($record->onetext === '');
- // Check operation ((210.10 + 39.92) - 150.02) against numeric types
+ // Check operation ((210.10 + 39.92) - 150.02) against numeric types.
$record->oneint = ((210.10 + 39.92) - 150.02);
$record->onenum = ((210.10 + 39.92) - 150.02);
$DB->update_record($tablename, $record);
$this->assertEquals(100, $record->oneint);
$this->assertEquals(100, $record->onenum);
- // Check various quotes/backslashes combinations in string types
+ // Check various quotes/backslashes combinations in string types.
$teststrings = array(
'backslashes and quotes alone (even): "" \'\' \\\\',
'backslashes and quotes alone (odd): """ \'\'\' \\\\\\',
$this->assertEquals($teststring, $record->onetext);
}
- // Check LOBs in text/binary columns
+ // Check LOBs in text/binary columns.
$clob = file_get_contents(__DIR__ . '/fixtures/clob.txt');
$blob = file_get_contents(__DIR__ . '/fixtures/randombinary');
$record->onetext = $clob;
$this->assertEquals($clob, $record->onetext, 'Test CLOB update (full contents output disabled)');
$this->assertEquals($blob, $record->onebinary, 'Test BLOB update (full contents output disabled)');
- // And "small" LOBs too, just in case
+ // And "small" LOBs too, just in case.
$newclob = substr($clob, 0, 500);
$newblob = substr($blob, 0, 250);
$record->onetext = $newclob;
$table = $this->get_test_table();
$tablename = $table->getName();
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('onechar', XMLDB_TYPE_CHAR, '100', null, null, null);
$table->add_field('onetext', XMLDB_TYPE_TEXT, 'big', null, null, null);
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$dbman->create_table($table);
- // simple set_field
+ // Simple set_field.
$id1 = $DB->insert_record($tablename, array('course' => 1));
$id2 = $DB->insert_record($tablename, array('course' => 1));
$id3 = $DB->insert_record($tablename, array('course' => 3));
$this->assertEquals(3, $DB->get_field($tablename, 'course', array('id' => $id3)));
$DB->delete_records($tablename, array());
- // multiple fields affected
+ // Multiple fields affected.
$id1 = $DB->insert_record($tablename, array('course' => 1));
$id2 = $DB->insert_record($tablename, array('course' => 1));
$id3 = $DB->insert_record($tablename, array('course' => 3));
$this->assertEquals(3, $DB->get_field($tablename, 'course', array('id' => $id3)));
$DB->delete_records($tablename, array());
- // no field affected
+ // No field affected.
$id1 = $DB->insert_record($tablename, array('course' => 1));
$id2 = $DB->insert_record($tablename, array('course' => 1));
$id3 = $DB->insert_record($tablename, array('course' => 3));
$this->assertEquals(3, $DB->get_field($tablename, 'course', array('id' => $id3)));
$DB->delete_records($tablename, array());
- // all fields - no condition
+ // All fields - no condition.
$id1 = $DB->insert_record($tablename, array('course' => 1));
$id2 = $DB->insert_record($tablename, array('course' => 1));
$id3 = $DB->insert_record($tablename, array('course' => 3));
$this->assertEquals(5, $DB->get_field($tablename, 'course', array('id' => $id2)));
$this->assertEquals(5, $DB->get_field($tablename, 'course', array('id' => $id3)));
- // test for exception throwing on text conditions being compared. (MDL-24863, unwanted auto conversion of param to int)
+ // Test for exception throwing on text conditions being compared. (MDL-24863, unwanted auto conversion of param to int).
$conditions = array('onetext' => '1');
try {
$DB->set_field($tablename, 'onechar', 'frog', $conditions);
if (debugging()) {
- // only in debug mode - hopefully all devs test code in debug mode...
+ // Only in debug mode - hopefully all devs test code in debug mode...
$this->fail('An Exception is missing, expected due to equating of text fields');
}
- } catch (exception $e) {
- $this->assertTrue($e instanceof dml_exception);
- $this->assertEquals($e->errorcode, 'textconditionsnotallowed');
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('dml_exception', $e);
+ $this->assertSame('textconditionsnotallowed', $e->errorcode);
}
// Test saving a float in a CHAR column, and reading it back.
$this->assertEquals(1e300, $DB->get_field($tablename, 'onetext', array('id' => $id)));
// Note: All the nulls, booleans, empties, quoted and backslashes tests
- // go to set_field_select() because set_field() is just one wrapper over it
+ // go to set_field_select() because set_field() is just one wrapper over it.
}
public function test_set_field_select() {
$table = $this->get_test_table();
$tablename = $table->getName();
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
- $table->add_field('oneint', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null);
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
+ $table->add_field('oneint', XMLDB_TYPE_INTEGER, '10', null, null, null);
$table->add_field('onenum', XMLDB_TYPE_NUMBER, '10,2', null, null, null);
$table->add_field('onechar', XMLDB_TYPE_CHAR, '100', null, null, null);
$table->add_field('onetext', XMLDB_TYPE_TEXT, 'big', null, null, null);
$this->assertTrue($DB->set_field_select($tablename, 'course', 2, 'id = ?', array(1)));
$this->assertEquals(2, $DB->get_field($tablename, 'course', array('id' => 1)));
- // Check nulls are set properly for all types
- $DB->set_field_select($tablename, 'oneint', null, 'id = ?', array(1)); // trues
+ // Check nulls are set properly for all types.
+ $DB->set_field_select($tablename, 'oneint', null, 'id = ?', array(1)); // Trues.
$DB->set_field_select($tablename, 'onenum', null, 'id = ?', array(1));
$DB->set_field_select($tablename, 'onechar', null, 'id = ?', array(1));
$DB->set_field_select($tablename, 'onetext', null, 'id = ?', array(1));
$this->assertNull($DB->get_field($tablename, 'onetext', array('id' => 1)));
$this->assertNull($DB->get_field($tablename, 'onebinary', array('id' => 1)));
- // Check zeros are set properly for all types
+ // Check zeros are set properly for all types.
$DB->set_field_select($tablename, 'oneint', 0, 'id = ?', array(1));
$DB->set_field_select($tablename, 'onenum', 0, 'id = ?', array(1));
$this->assertEquals(0, $DB->get_field($tablename, 'oneint', array('id' => 1)));
$this->assertEquals(0, $DB->get_field($tablename, 'onenum', array('id' => 1)));
- // Check booleans are set properly for all types
- $DB->set_field_select($tablename, 'oneint', true, 'id = ?', array(1)); // trues
+ // Check booleans are set properly for all types.
+ $DB->set_field_select($tablename, 'oneint', true, 'id = ?', array(1)); // Trues.
$DB->set_field_select($tablename, 'onenum', true, 'id = ?', array(1));
$DB->set_field_select($tablename, 'onechar', true, 'id = ?', array(1));
$DB->set_field_select($tablename, 'onetext', true, 'id = ?', array(1));
$this->assertEquals(1, $DB->get_field($tablename, 'onechar', array('id' => 1)));
$this->assertEquals(1, $DB->get_field($tablename, 'onetext', array('id' => 1)));
- $DB->set_field_select($tablename, 'oneint', false, 'id = ?', array(1)); // falses
+ $DB->set_field_select($tablename, 'oneint', false, 'id = ?', array(1)); // Falses.
$DB->set_field_select($tablename, 'onenum', false, 'id = ?', array(1));
$DB->set_field_select($tablename, 'onechar', false, 'id = ?', array(1));
$DB->set_field_select($tablename, 'onetext', false, 'id = ?', array(1));
$this->assertEquals(0, $DB->get_field($tablename, 'onechar', array('id' => 1)));
$this->assertEquals(0, $DB->get_field($tablename, 'onetext', array('id' => 1)));
- // Check string data causes exception in numeric types
+ // Check string data causes exception in numeric types.
try {
$DB->set_field_select($tablename, 'oneint', 'onestring', 'id = ?', array(1));
$this->fail("Expecting an exception, none occurred");
- } catch (exception $e) {
- $this->assertTrue($e instanceof dml_exception);
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('dml_exception', $e);
}
try {
$DB->set_field_select($tablename, 'onenum', 'onestring', 'id = ?', array(1));
$this->fail("Expecting an exception, none occurred");
- } catch (exception $e) {
- $this->assertTrue($e instanceof dml_exception);
+ } catch (moodle_exception $e) {
+ $this->assertInstanceOf('dml_exception', $e);
}
- // Check empty string data is stored as 0 in numeric datatypes
+ // Check empty string data is stored as 0 in numeric datatypes.
$DB->set_field_select($tablename, 'oneint', '', 'id = ?', array(1));
$field = $DB->get_field($tablename, 'oneint', array('id' => 1));
$this->assertTrue(is_numeric($field) && $field == 0);