Esempio n. 1
0
 public function testBelongsToIndex()
 {
     Cache::flush();
     Schema\Builder::getInstance()->migrate(App::collection('auths')->getModel(), array('attributes' => array(array('name' => 'lucky_number_id', 'type' => 'integer', 'unique' => true)), 'relationships' => array('belongs_to' => 'lucky_numbers')));
     $auths_cache = SchemaCache::get('auths');
     $this->assertTrue($auths_cache['attributes'][0]['name'] == 'lucky_number_id');
 }
Esempio n. 2
0
 public function testBulkCreate()
 {
     $models = App::collection('my_items')->create(array(array('name' => "One"), array('name' => "Two"), array('name' => "Three")));
     $this->assertTrue($models[0]->name == "One");
     $this->assertTrue($models[1]->name == "Two");
     $this->assertTrue($models[2]->name == "Three");
 }
Esempio n. 3
0
 public function testDataTypes()
 {
     App::collection('dummy')->create(array('boolean' => true, 'number' => 1, 'float' => 5.5, 'string' => "Strings"));
     $data = App::collection('dummy')->first()->toArray();
     $this->assertTrue(is_int($data['number']));
     $this->assertTrue(is_float($data['float']));
     $this->assertTrue(is_string($data['string']));
     $this->assertTrue(is_bool($data['boolean']));
 }
Esempio n. 4
0
 public function testCache()
 {
     App::collection('my_items')->create(array('name' => "Cached"));
     $first_item = App::collection('my_items')->where('name', "Cached")->remember(10)->get();
     $this->assertTrue($first_item[0]->name == "Cached");
     App::collection('my_items')->update(array('name' => "Not cached!"));
     $first_item = App::collection('my_items')->where('name', "Cached")->remember(10)->get();
     $this->assertTrue($first_item[0]->name == "Cached");
 }
 public function notify()
 {
     if (!(Context::getKey()->isServer() && Request::header('X-Scheduled-Task'))) {
         throw new ForbiddenException("Need a 'device' key to perform this action.");
     }
     $notifier = new PushNotification\Notifier();
     $messages = Model\App::collection('push_messages')->where('status', Model\PushMessage::STATUS_QUEUE);
     return $notifier->push_messages($messages);
 }
Esempio n. 6
0
 public function testModifyField()
 {
     Schema\Builder::getInstance()->migrate(App::collection('modify')->getModel(), array('attributes' => array(array('name' => "field", 'type' => "string"))));
     App::collection('modify')->create(array('field' => "5"));
     $data = App::collection('modify')->first()->toArray();
     $this->assertTrue($data['field'] == "5");
     Schema\Builder::getInstance()->migrate(App::collection('modify')->getModel(), array('attributes' => array(array('name' => "field", 'type' => "integer", 'index' => true))));
     $data = App::collection('modify')->first()->toArray();
     $this->assertTrue($data['field'] == 5);
 }
Esempio n. 7
0
 public function testMigrateFields()
 {
     Cache::flush();
     $attributes = array(array('name' => "default_is_string"), array('name' => "string", 'type' => "string"), array('name' => "int", 'type' => "integer"), array('name' => "float", 'type' => "float"), array('name' => "boolean", 'type' => "boolean"));
     // books / authors / contacts
     Schema\Builder::getInstance()->migrate(App::collection('schema')->getModel(), array('attributes' => $attributes));
     $dump = Schema\Builder::getInstance()->dump();
     $this->assertTrue(count($dump['schemas']['attributes']) == 5);
     $this->assertTrue($dump['schemas']['attributes'] == $attributes);
 }
Esempio n. 8
0
 public function beforeCreate()
 {
     // cache Auth role for this token
     //
     // TODO: use auth() relationship.
     // Due the same problem at Auth::current(), it was needed to use
     // App::collection here
     //
     $this->role = App::collection('auth')->where('_id', $this->auth_id)->first()->role;
     $this->created_at = Carbon::now();
     $token_expiration = Config::get('auth.token_expiration', static::DEFAULT_TOKEN_EXPIRATION);
     $this->expire_at = Carbon::now()->addHours($token_expiration);
     $this->token = sha1(uniqid(rand(), true));
 }
Esempio n. 9
0
 public function testOwnerReadSuccess()
 {
     $this->setConfig(App::collection('restricted_content')->getTable(), 'read', 'owner');
     $auth_id = 1;
     App::collection('restricted_content')->create(array('name' => "Read success", 'auth_id' => $auth_id));
     App::collection('restricted_content')->create(array('name' => "Read fail", 'auth_id' => 2));
     // mock authorized user
     $auth_token = new AuthToken(array('auth_id' => $auth_id));
     AuthToken::setCurrent($auth_token);
     $this->assertTrue(is_array(App::collection('restricted_content')->where('auth_id', 1)->first()->toArray()));
     // wrong auth_id, throw exception
     $this->setExpectedException('Hook\\Exceptions\\NotAllowedException');
     App::collection('restricted_content')->where('auth_id', 2)->first()->toArray();
 }
Esempio n. 10
0
 /**
  * get
  *
  * @param mixed $model
  * @param string $relation_name
  *
  * @return \Illuminate\Database\Eloquent\Relations\Relation
  */
 public static function getRelationInstance($model, $relation_type, $field, $config)
 {
     $related_collection = App::collection($config['collection']);
     $model_table = str_singular($model->getTable());
     $related_model = $related_collection->getModel();
     $related_table = $related_model->getTable();
     $primary_key = $config['primary_key'];
     $foreign_key = $config['foreign_key'];
     // define relation model
     $related_klass = "Related" . ucfirst(str_singular(camel_case($field))) . ucfirst(str_singular(camel_case($related_table)));
     // FIXME:
     // eval is evil. But it's necessary here since Eloquent\Model
     // will try to instantiate the 'related class' without constructor params.
     if (!class_exists($related_klass)) {
         $related_model_class = get_class($related_model);
         eval("class {$related_klass} extends {$related_model_class} { protected \$table = '{$related_table}'; }");
     }
     // FIXME: refactoring
     // force table name on related query instance.
     $related_instance = new $related_klass();
     $related_instance->getModel()->setTable($related_table);
     $related_query = $related_instance->getModel()->newQuery();
     switch ($relation_type) {
         case "belongs_to":
             return new BelongsTo($related_query, $model, $foreign_key, $primary_key, $field);
         case "belongs_to_many":
             return new BelongsToMany($related_query, $model, $related_table, $foreign_key, $primary_key, $field);
         case "has_many":
             if (isset($config['through'])) {
                 $through = App::collection($config['through'])->getModel();
                 $first_key = $foreign_key;
                 $second_key = isset($config['far_key']) ? $config['far_key'] : str_singular($config['collection']) . '_id';
                 $local_key = isset($config['local_key']) ? $config['local_key'] : '_id';
                 return new HasManyThrough($related_query, $model, $through, $first_key, $second_key, $local_key);
             } else {
                 return new HasMany($related_query, $model, $foreign_key, $primary_key);
             }
         case "has_one":
             // hasOne($related, $foreignKey = null, $localKey = null)
             // $model->hasOne($related_klass, $model_table . '_id', '_id');
             return new HasOne($related_query, $model, $foreign_key, $primary_key);
         default:
             return new NotImplementedException("'{$relation_type}' is not implemented. Please use 'belongs_to', 'has_many' or 'belongs_to_many'.");
     }
     return null;
 }
Esempio n. 11
0
 /**
  * push
  * @param Hook\Model\PushMessage $message
  */
 public function push($message)
 {
     $status = array('success' => 0, 'failure' => 0);
     foreach (static::getPlatformServices() as $platform => $service_klass) {
         $service = new $service_klass();
         $query = App::collection('push_registrations')->where('platform', $platform);
         $query->chunk(self::MAX_RECIPIENTS_PER_REQUEST, function ($registrations) use(&$platform, &$service, &$status, $message) {
             try {
                 $chunk_status = $service->push($registrations, $message);
                 $status['success'] += $chunk_status['success'];
                 $status['failure'] += $chunk_status['failure'];
             } catch (\Exception $e) {
                 Logger::debug("PushNotification: platform: {$platform} -> {$e->getMessage()}");
             }
         });
     }
     return $status;
 }
Esempio n. 12
0
 public function show404()
 {
     if (Request::isPost()) {
         $page = App::collection(self::PAGES_COLLECTION)->create(Input::get('page'));
         Request::redirect($page->slug);
     }
     $layouts = array();
     $layout_files = glob(Router::config('paths')['root'] . 'app/views/layouts/*');
     foreach ($layout_files as $layout_file) {
         $layout_ext = pathinfo($layout_file, PATHINFO_EXTENSION);
         $layout_name = basename($layout_file, '.' . $layout_ext);
         $words = preg_split('/_/', $layout_name);
         $layout_label = join(' ', array_map(function ($word) {
             return ucfirst($word);
         }, $words));
         $layouts[$layout_name] = $layout_label;
     }
     $this->render('cms/404', array('request_path' => Request::path(), 'layouts' => $layouts));
 }
Esempio n. 13
0
 public function upload_schema()
 {
     $schema = Input::get();
     foreach ($schema as $collection => $config) {
         Schema\Builder::getInstance()->migrate(Model\App::collection($collection)->getModel(), $config);
     }
     return array('success' => true);
 }
Esempio n. 14
0
 public function delete($name, $_id = null)
 {
     $collection = Model\App::collection($name);
     $success = false;
     // trusted context:
     // run a real truncate statement if performing a delete
     if (Context::isTrusted() && $_id == null && count(Input::get('q')) == 0) {
         $success = $collection->truncate();
     } else {
         // untrusted context:
         // remove a single row, or the items from a filter in
         $query = $_id ? $collection->find($_id) : $collection->filter(Input::get('q'));
         $success = $query->delete();
     }
     return array('success' => $success);
 }
Esempio n. 15
0
 public function testMigrateAddRelationship()
 {
     Schema\Builder::getInstance()->migrate(App::collection('schema_cache')->getModel(), array('relationships' => array('has_many' => array('contacts'))));
     Schema\Builder::getInstance()->migrate(App::collection('schema_cache')->getModel(), array('relationships' => array('has_many' => array('contacts'))));
     $this->assertTrue(SchemaCache::get('schema_caches') == array('attributes' => array(), 'relationships' => array('has_many' => array('contacts' => array('collection' => 'contacts', 'foreign_key' => 'schema_cach_id', 'primary_key' => '_id'))), 'lock_attributes' => false));
 }
Esempio n. 16
0
/**
 * collection
 * @return string
 */
function collection($name)
{
    return App::collection($name);
}
Esempio n. 17
0
 public function testDirectAssociation()
 {
     $match_1 = App::collection('matches')->create(array('name' => "Team one VS Team two", 'house' => array('name' => "One"), 'guest' => array('name' => "Two")));
     $three = App::collection('teams')->create(array('name' => "Three"));
     $four = App::collection('teams')->create(array('name' => "Four"));
     $match_2 = App::collection('matches')->create(array('name' => "Team three VS Team four", 'house' => $three, 'guest' => $four));
     // retrieve recent-created matches with relationships
     $matches = App::collection('matches')->where('_id', '>=', $match_1->_id)->join('house', 'guest')->toArray();
     $this->assertTrue($matches[0]['house']['name'] == "One");
     $this->assertTrue($matches[0]['guest']['name'] == "Two");
     $this->assertTrue($matches[1]['house']['name'] == "Three");
     $this->assertTrue($matches[1]['guest']['name'] == "Four");
     // from related direction
     App::collection('authors')->create(array('name' => "Somebody", 'contacts' => array(array('name' => "Contact 1"), array('name' => "Contact 2"), array('name' => "Contact 3"), array('name' => "Contact 4"), array('name' => "Contact 5"))));
     $this->markTestIncomplete("This feature doesn't work yet.");
     // $authors_and_contacts = App::collection('authors')->join('contacts')->toArray();
     // $this->assertTrue(count($authors_and_contacts) == 2);
     // $this->assertTrue(count($authors_and_contacts[0]['contacts']) == 2);
     // $this->assertTrue(count($authors_and_contacts[1]['contacts']) == 5);
 }
Esempio n. 18
0
 /**
  * Handle Illuminate\Database\Query\Builder methods.
  *
  * @param  mixed                        $method     method
  * @param  mixed                        $parameters parameters
  * @return Database\CollectionDelegator
  */
 public function __call($method, $parameters)
 {
     try {
         $mixed = call_user_func_array(array($this->query, $method), $parameters);
     } catch (\BadMethodCallException $e) {
         $model = App::collection($this->name)->getModel();
         $mixed = call_user_func_array(array($model, $method), $parameters);
     }
     if ($mixed instanceof \Illuminate\Database\Eloquent\Builder || $mixed instanceof \Illuminate\Database\Query\Builder) {
         return $this;
     } else {
         return $mixed;
     }
 }
Esempio n. 19
0
 public function index($name)
 {
     $name = implode("/", $name);
     return Model\App::collection('channel_messages')->filter(Input::get('q'))->where('channel', $name)->get();
 }