public static function run() { $class = get_called_class(); TEST($class, "{$class}.student.saveEditSubmitAndDelete", function ($class) { // Create job global $MJob; $jobId = new MongoId($MJob->save([], false)); $studentId = new MongoId(); // make sure saving an application for a job with no application // fails EQ(null, ApplicationStudent::save($jobId, $studentId, [])); // create application for job $questionId = new MongoId(); $questions = [$questionId]; ApplicationJob::createOrUpdate($jobId, $questions); $answers = [['_id' => $questionId, 'answer' => 'swag']]; $firstApplication = ApplicationStudent::save($jobId, $studentId, $answers); NEQ(null, $firstApplication); $applicationId = $firstApplication->getId(); NEQ(null, $firstApplication->getQuestions()); EQ('swag', $firstApplication->getQuestions()[0]['answer']); // edit application ApplicationStudent::edit($applicationId, [['_id' => $questionId, 'answer' => 'qswag']]); $editedApplication = ApplicationModel::getSavedForJob($jobId)[0]; EQ('qswag', $editedApplication['questions'][0]['answer']); // submit application TRUE(ApplicationStudent::submitSaved($applicationId)); TRUE(ApplicationModel::checkSubmitted($applicationId)); // submit new application $secondStudent = new MongoId(); $secondApplication = ApplicationStudent::submitNew($jobId, $secondStudent, $answers); TRUE(ApplicationModel::checkSubmitted($secondApplication->getId())); // create, save, and delete application $thirdStudent = new MongoId(); $applicationToDelete = ApplicationStudent::save($jobId, $thirdStudent, $answers)->getId(); TRUE(ApplicationModel::applicationExists($jobId, $thirdStudent)); TRUE(ApplicationStudent::deleteSaved($applicationToDelete)); }); TEST($class, "{$class}.student.getUnclaimedAndClaimed", function ($class) { TRUE(false, 'TODO: Write test'); }); TEST($class, "{$class}.job.createOrUpdate", function ($class) { // Try to create/update application for nonexistent job FALSE(ApplicationJob::createOrUpdate(new MongoId(), [])); // Create job global $MJob; $jobId = new MongoId($MJob->save([], false)); // Try to create application for job $oldQuestionId = new MongoId(); $oldQuestions = [$oldQuestionId]; TRUE(ApplicationJob::createOrUpdate($jobId, $oldQuestions)); EQ($MJob::getApplicationQuestionIds($jobId), $oldQuestions); // Try to change one question in application for job $newQuestionId = new MongoId(); $newQuestions = [$newQuestionId]; TRUE(ApplicationJob::createOrUpdate($jobId, $newQuestions)); EQ($MJob::getApplicationQuestionIds($jobId), $newQuestions); TRUE(false, 'TODO: Check to make sure student applications are updated right.'); }); }
public static function run() { $class = get_called_class(); TEST($class, "{$class}.correctCollection", function () { $actualCollection = StudentModel::myCollection(); $correctCollection = 'emails'; EQ($actualCollection, $correctCollection, "Collection name invalid: {$actualCollection}"); }); TEST($class, "{$class}.save", function () { $id = self::$MStudentTest->save(array()); TRUE(self::$MStudentTest->exists($id)); }); }
/** * private function _set_relationships() * * Called by the public method with() it will set the relationships between the current model and other models */ private function _set_relationships() { if (empty($this->_relationships)) { $options = array('has_one', 'has_many', 'has_many_pivot'); foreach ($options as $option) { if (isset($this->{$option}) && !empty($this->{$option})) { foreach ($this->{$option} as $key => $relation) { if (!is_array($relation)) { $foreign_model = $relation; $foreign_model_name = strtolower($foreign_model); $this->load->model($foreign_model_name); $foreign_table = $this->{$foreign_model_name}->table; $foreign_key = $this->{$foreign_model_name}->primary_key; $local_key = $this->primary_key; $pivot_local_key = $this->table . '_' . $local_key; $pivot_foreign_key = $foreign_table . '_' . $foreign_key; $get_relate = FALSE; } else { if ($this->_is_assoc($relation)) { $foreign_model = $relation['foreign_model']; if (array_key_exists('foreign_table', $relation)) { $foreign_table = $relation['foreign_table']; } else { $foreign_model_name = strtolower($foreign_model); $this->load->model($foreign_model_name); $foreign_table = $this->{$foreign_model_name}->table; } $foreign_key = $relation['foreign_key']; $local_key = $relation['local_key']; if ($option == 'has_many_pivot') { $pivot_table = $relation['pivot_table']; $pivot_local_key = array_key_exists('pivot_local_key', $relation) ? $relation['pivot_local_key'] : $this->table . '_' . $this->primary_key; $pivot_foreign_key = array_key_exists('pivot_foreign_key', $relation) ? $relation['pivot_foreign_key'] : $foreign_table . '_' . $foreign_key; $get_relate = array_key_exists('get_relate', $relation) && $relation['get_relate'] === TRUE ? TRUE : FALSE; } } else { $foreign_model = $relation[0]; $foreign_model_name = strtolower($foreign_model); $this->load->model($foreign_model_name); $foreign_table = $this->{$foreign_model_name}->table; $foreign_key = $relation[1]; $local_key = $relation[2]; if ($option == 'has_many_pivot') { $pivot_local_key = $this->table . '_' . $this->primary_key; $pivot_foreign_key = $foreign_table . '_' . $foreign_key; $get_relate = isset($relation[3]) && $relation[3] === TRUE() ? TRUE : FALSE; } } } if ($option == 'has_many_pivot' && !isset($pivot_table)) { $tables = array($this->table, $foreign_table); sort($tables); $pivot_table = $tables[0] . '_' . $tables[1]; } $this->_relationships[$key] = array('relation' => $option, 'relation_key' => $key, 'foreign_model' => strtolower($foreign_model), 'foreign_table' => $foreign_table, 'foreign_key' => $foreign_key, 'local_key' => $local_key); if ($option == 'has_many_pivot') { $this->_relationships[$key]['pivot_table'] = $pivot_table; $this->_relationships[$key]['pivot_local_key'] = $pivot_local_key; $this->_relationships[$key]['pivot_foreign_key'] = $pivot_foreign_key; $this->_relationships[$key]['get_relate'] = $get_relate; } } } } } }
function NEQ($val1, $val2, $errorMessage = '') { TRUE($val1 != $val2, $errorMessage); }