protected function _load_user($user) { if (!$user) { return NULL; } return is_object($user) ? $user : Jam::find('test_user', $user); }
public function get_jam() { if (Request::ajax()) { $jam = Input::get('jam'); $waktu = Jam::find($jam); echo $waktu->dari_jam . "|" . $waktu->sampai_jam; } }
/** * @covers Jam_Behavior_Promotable_Purchase::model_after_load */ public function test_model_after_load() { $purchase = Jam::find('purchase', 1); $promo_code = Jam::find('promo_code', 2); $purchase->promo_code = $promo_code; $purchase->save(); $purchase = Jam::find('purchase', 1); $this->assertEquals($promo_code->code, $purchase->promo_code_text); }
/** * @covers ::load */ public function test_load_loaded_user2() { $user = Jam::find('user', 2); Auth::instance()->force_login($user); $result = Model_Visitor::load(); $this->assertSame($result->user, $user); $this->assertTrue($result->loaded()); $this->assertNotEquals(1, $result->id()); }
/** * If there is a new value in promo_code_text, try to load promo_code object. * If the new value is NULL, remove it * @param Model_Purchase $purchase */ public function model_after_check(Model_Purchase $purchase) { if ($purchase->changed('promo_code_text') and !$purchase->errors('promo_code_text')) { if ($purchase->promo_code_text) { $purchase->promo_code = Jam::find('promo_code', $purchase->promo_code_text); } else { $purchase->promo_code = NULL; } } }
/** * @covers Kohana_Jam_Association_Materializedpath_Belongsto::model_after_save */ public function test_belongsto() { $two = Jam::find('category', 2); $seven = Jam::find('category', 7); $two->parent = $seven; $two->save(); $result = Jam::all('category')->as_array('id', 'path'); $expected = array(1 => NULL, 2 => '1/3/6/7', 3 => '1', 4 => '1/3/6/7/2', 5 => '1/3/6/7/2', 6 => '1/3', 7 => '1/3/6', 8 => '1/3/6'); $this->assertEquals($expected, $result); }
public function external_data_for(Model_Location $location) { $key = $this->generate_data_key($location); $external_data = Jam::find('shipping_external_data', $key); if (!$external_data) { // TODO add proper external shipping API logic here $external_data = Jam::build('shipping_external_data', array('key' => $key, 'price' => new Jam_Price(5.13, 'GBP'), 'delivery_time' => new Jam_Range(array(3, 5)))); $external_data->save(); } return $external_data; }
public function test_validate() { $element = Jam::find('test_element', 1); Jam::validator_rule('unique', array())->validate($element, 'name', $element->name); $this->assertNotHasError($element, 'name', 'unique'); $element2 = Jam::find('test_element', 2); Jam::validator_rule('unique', array())->validate($element2, 'url', $element2->url); $this->assertHasError($element2, 'url', 'unique'); $element3 = Jam::find('test_element', 2); Jam::validator_rule('unique', array('scope' => 'name'))->validate($element3, 'url', $element3->url); $this->assertNotHasError($element3, 'url', 'unique'); }
/** * @covers Jam_Behavior_Location_Parent::model_before_check */ public function test_model_before_check() { $france = Jam::find('location', 'France'); $address = Jam::build('address'); $address->city = Jam::build('location', array('name' => 'Paris')); $address->country = $france; $address->check(); $this->assertEquals($france, $address->city->parent); $address->save(); $paris = Jam::find('location', 'Paris'); $this->assertEquals($france, $paris->parent); }
/** * @covers Model_Promotion_Promocode::has_promo_code */ public function test_has_promo_code() { $promotion1 = Jam::find('promotion', 1); $promotion2 = Jam::find('promotion', 2); $promo_code1 = Jam::find('promo_code', 1); $promo_code2 = Jam::find('promo_code', 2); $promo_code3 = Jam::find('promo_code', 3); $this->assertTrue($promotion1->has_promo_code($promo_code1)); $this->assertFalse($promotion1->has_promo_code($promo_code2)); $this->assertTrue($promotion2->has_promo_code($promo_code2)); $this->assertTrue($promotion2->has_promo_code($promo_code3)); }
public function build_user($data, $create = TRUE) { if ($this->logged_in() and !empty($data)) { $user = Jam::build($this->_user_model); if ($user->load_service_values($this, $data, $create) === FALSE) { return FALSE; } $user->roles->add(Jam::find($this->_role_model, 'login')); $user->set($this->_service_field, $this->service_uid()); return $user; } }
public function test_get() { $one = Jam::find('test_closurelist', 1); $four = Jam::find('test_closurelist', 4); $two = Jam::find('test_closurelist', 2); // Check for Null Value changed $this->assertNull($this->association->get($one, NULL, TRUE)); $this->assertEquals($one->id(), $this->association->get($four, NULL, FALSE)->id()); $this->assertEquals($two->id(), $this->association->get($four, 2, TRUE)->id()); $returned = $this->association->get($four, array('id' => 2, 'title' => 'new post title'), TRUE); $this->assertEquals($two->id(), $returned->id()); $this->assertEquals('new post title', $returned->title); }
public function action_remoteselect_template() { $params = $this->request->query(); if ($missing = array_diff(array('model', 'id', 'name', 'template'), array_keys($params))) { throw new Kohana_Exception('You must provide :params query parameter', array(':params' => join(', ', $params))); } $params['item'] = Jam::find($params['model'], $params['id']); if (!$params['item']) { $this->response->body(''); } else { $view = View::factory(Arr::get($params, 'template'), $params); $this->response->body($view->render()); } }
/** * @covers Jam_Behavior_Shippable_Purchase::add_item */ public function test_add_item() { $purchase = Jam::find('purchase', 2); $france = Jam::find('location', 'France'); $purchase->shipping_country($france); $product = Jam::find('product', 2); $perchase_item = Jam::build('purchase_item_product', array('reference' => $product)); $purchase->add_item($product->brand, $perchase_item); $item = $purchase->brand_purchases[0]->shipping->items[0]; $this->assertInstanceOf('Model_Shipping_Item', $item); $this->assertSame($france, $purchase->brand_purchases[0]->shipping->ship_to()); $this->assertSame($perchase_item, $item->purchase_item); $this->assertInstanceOf('Model_Shipping_Group', $item->shipping_group); }
public function test_collect_children() { $model = Jam::find('test_blog', 1); $collected = Jam_Behavior_Cascade::collect_models($model, array('test_posts')); $this->assertEquals((string) $model, (string) $collected[0]); foreach ($model->test_posts as $i => $post) { $this->assertEquals((string) $post, (string) $collected[$i + 1]); } $collected = Jam_Behavior_Cascade::collect_models($model, array('test_posts' => array('test_author', 'test_tags', 'test_images'))); $expected = array($model, $model->test_posts[0], $model->test_posts[0]->test_author, $model->test_posts[0]->test_tags[0], $model->test_posts[0]->test_tags[1], $model->test_posts[0]->test_tags[2], $model->test_posts[0]->test_tags[3], $model->test_posts[0]->test_tags[4], $model->test_posts[0]->test_images[0]); foreach ($expected as $i => $item) { $this->assertEquals((string) $item, (string) $collected[$i], 'Collection item ' . $i . ' must be "' . $item . '"'); } }
/** * @covers Jam_Behavior_Promotable_Brand_Purchase::filter_promotion_items */ public function test_filter_promotion_items() { $promocode_giftcard = Jam::build('promotion_promocode_giftcard', array('id' => 20)); $promocode_percent = Jam::build('promotion_promocode_percent', array('id' => 21)); $brand_purchase = Jam::find('brand_purchase', 2); $brand_purchase->items->add(Jam::build('purchase_item_promotion', array('id' => 12, 'reference' => $promocode_giftcard, 'is_discount' => TRUE, 'is_payable' => TRUE))); $brand_purchase->items->add(Jam::build('purchase_item_promotion', array('id' => 16, 'reference' => $promocode_percent, 'is_discount' => TRUE, 'is_payable' => TRUE))); $items = $brand_purchase->items(array('promotion' => 'promocode_giftcard')); $this->assertEquals(array(12), $this->ids($items)); $items = $brand_purchase->items(array('promotion' => 'promocode_percent')); $this->assertEquals(array(16), $this->ids($items)); $items = $brand_purchase->items(array('promotion' => array('promocode_percent', 'promocode_giftcard'))); $this->assertEquals(array(12, 16), $this->ids($items)); $items = $brand_purchase->items(array('promotion', 'not_promotion' => 'promocode_giftcard')); $this->assertEquals(array(16), $this->ids($items)); }
public function test_hasone_deep() { $author = Jam::find('test_author', 1); $this->assertInstanceOf('Model_Test_Post', $author->test_post); $this->assertEquals(1, $author->test_post->id()); $author->test_post = array('id' => 2, 'name' => 'changed post'); $author->save(); $this->assertInstanceOf('Model_Test_Post', $author->test_post); $this->assertEquals(2, $author->test_post->id()); $this->assertEquals('changed post', $author->test_post->name()); $author = Jam::find('test_author', 1); $this->assertInstanceOf('Model_Test_Post', $author->test_post); $this->assertEquals(2, $author->test_post->id()); $author->test_post = array('id' => 3, 'name' => 'changed post'); $author->save(); $this->assertInstanceOf('Model_Test_Post', $author->test_post); $this->assertEquals(3, $author->test_post->id()); $this->assertEquals('changed post', $author->test_post->name()); }
public function test_capture_exception() { $sentry = $this->getMock('Service_Sentry', array('initialized', 'send_exception_with_user_data'), array('sentry')); $user = Jam::find('user', 1); $exception = new Exception('test'); $data = array('data' => 'testdata'); $sentry->expects($this->exactly(4))->method('initialized')->will($this->onConsecutiveCalls(FALSE, TRUE, TRUE, TRUE)); $sentry->expects($this->at(2))->method('send_exception_with_user_data')->with($this->identicalTo($exception), $this->equalTo(NULL), $this->equalTo(NULL), $this->equalTo(array()))->will($this->returnValue('test_id_2')); $sentry->expects($this->at(4))->method('send_exception_with_user_data')->with($this->identicalTo($exception), $this->equalTo($user->id()), $this->equalTo($user->email), $this->equalTo(array()))->will($this->returnValue('test_id_3')); $sentry->expects($this->at(6))->method('send_exception_with_user_data')->with($this->identicalTo($exception), $this->equalTo($user->id()), $this->equalTo($user->email), $this->equalTo($data))->will($this->returnValue('test_id_4')); $result_1 = $sentry->capture_exception($exception); $this->assertNull($result_1); $result_2 = $sentry->capture_exception($exception); $this->assertEquals('test_id_2', $result_2); Auth::instance()->force_login($user); $result_3 = $sentry->capture_exception($exception); $this->assertEquals('test_id_3', $result_3); $result_4 = $sentry->capture_exception($exception, $data); $this->assertEquals('test_id_4', $result_4); }
/** * @dataProvider data_construct * @covers Jam_Field_Ip::get * @covers Jam_Field_Ip::convert */ public function test_construct($ip, $expected) { $this->env->backup_and_set(array('Request::$client_ip' => $ip)); $user = Jam::build('user'); $this->assertEquals($expected, $user->ip); $user->ip = 'TEST'; $this->assertEquals('TEST', $user->ip); $user->ip = NULL; $this->assertEquals(NULL, $user->ip); $user->save(); $user = Jam::find('user', $user->id()); $this->assertEquals(NULL, $user->ip); $user->ip = $ip; $user->save(); $user = Jam::find('user', $user->id()); $this->assertEquals($expected, $user->ip); $user = Jam::build('user'); $user->save(); $user = Jam::find('user', $user->id()); $this->assertEquals($expected, $user->ip); }
/** * @covers Jam_Validator_Rule_Promocode::validate */ public function test_validate() { $model = Jam::find('product', 1); $model2 = Jam::find('product', 1); $model3 = Jam::find('product', 1); $promo_code = Jam::build('promo_code'); $expired_promo_code = Jam::build('promo_code', array('expires_at' => date('Y-m-d H:i:s', strtotime('-1 month')))); $validator_rule = $this->getMock('Jam_Validator_Rule_Promocode', array('valid_promo_code'), array(array())); $validator_rule->expects($this->exactly(3))->method('valid_promo_code')->with($this->equalTo('PROMOCODE'))->will($this->onConsecutiveCalls(NULL, $promo_code, $expired_promo_code)); $validator_rule->validate($model, 'promo_code', 'PROMOCODE'); $this->assertFalse($model->is_valid()); $errors = $model->errors()->as_array(); $expected = array('promo_code_text' => array('invalid' => array())); $this->assertEquals($expected, $errors); $validator_rule->validate($model2, 'promo_code', 'PROMOCODE'); $this->assertTrue($model2->is_valid()); $validator_rule->validate($model3, 'promo_code', 'PROMOCODE'); $this->assertFalse($model3->is_valid()); $errors = $model3->errors()->as_array(); $expected = array('promo_code_text' => array('expired' => array())); $this->assertEquals($expected, $errors); }
/** * Get the belonging model for this association using the foreign key, * if the data was changed, use the key from the changed data. * Assign inverse_of * * @param Jam_Validated $model * @param mixed $value changed data * @param boolean $is_changed * @return Jam_Model */ public function get(Jam_Validated $model, $value, $is_changed) { if ($is_changed) { if ($value instanceof Jam_Validated or !$value) { return $value; } $key = Jam_Association::primary_key($this->foreign_model, $value); if ($key) { $item = Jam::find($this->foreign_model, $key); } elseif (is_array($value)) { $item = Jam::build($this->foreign_model); } else { $item = NULL; } if ($item and is_array($value)) { $item->set($value); } } else { $item = Jam::all($this->foreign_model)->join_table($this->branches_table)->on($this->ansestor_key, '=', ':primary_key')->on($this->depth_key, '=', DB::expr(':depth', array(':depth' => 1)))->end()->where($this->branches_table . '.' . $this->descendant_key, '=', $model->id())->first(); } return $this->set($model, $item, $is_changed); }
/** * Tests that primary keys can be changed or set manually. * * We don't put this in the PrimaryTest because it has more * to do with how the model handles it than the field. */ public function test_save_primary_key() { $model = Jam::build('test_post'); $model->id = 9000; $model->save(); // Verify data is as it should be $this->assertTrue($model->saved()); $this->assertEquals(9000, $model->id); // Verify the record actually exists in the database $this->assertNotNull(Jam::find('test_post', 9000)); // Manually re-selecting so that Postgres doesn't cause errors down the line $model = Jam::find('test_post', 9000); // Change it again so we can verify it works on UPDATE as well // This is key because Jam got this wrong in the past $model->id = 9001; $model->save(); // Verify we can't find the old record 9000 $this->assertNull(Jam::find('test_post', 9000)); // And that we can find the new 9001 $this->assertNotNull(Jam::find('test_post', 9001)); // Cleanup Jam::find('test_post', 9001)->delete(); }
/** * @covers Jam_Validator_Rule_Purchase_Promocode::validate */ public function test_validate() { $purchase = Jam::find('purchase', 1); $purchase2 = Jam::find('purchase', 1); $purchase3 = Jam::find('purchase', 1); $promo_code = $this->getMock('Model_Promo_Code', array('validate_purchase'), array('promo_code')); $promo_code->expects($this->once())->method('validate_purchase')->with($this->identicalTo($purchase2)); $expired_promo_code = Jam::build('promo_code', array('expires_at' => date('Y-m-d H:i:s', strtotime('-1 month')))); $validator_rule = $this->getMock('Jam_Validator_Rule_Purchase_Promocode', array('valid_promo_code'), array(array())); $validator_rule->expects($this->exactly(3))->method('valid_promo_code')->with($this->equalTo('PROMOCODE'), $this->isInstanceOf('Model_Purchase'))->will($this->onConsecutiveCalls(NULL, $promo_code, $expired_promo_code)); $validator_rule->validate($purchase, 'promo_code', 'PROMOCODE'); $this->assertFalse($purchase->is_valid()); $errors = $purchase->errors()->as_array(); $expected = array('promo_code_text' => array('invalid' => array())); $this->assertEquals($expected, $errors); $validator_rule->validate($purchase2, 'promo_code', 'PROMOCODE'); $this->assertTrue($purchase2->is_valid()); $validator_rule->validate($purchase3, 'promo_code', 'PROMOCODE'); $this->assertFalse($purchase3->is_valid()); $errors = $purchase3->errors()->as_array(); $expected = array('promo_code_text' => array('expired' => array())); $this->assertEquals($expected, $errors); }
public function test_with_filter() { $video = Jam::find('test_video', 1); $video->delete(); $this->assertCount(3, Jam::all('test_video')); $result = Jam_Behavior_Paranoid::with_filter(Jam_Behavior_Paranoid::ALL, function () { return Jam::all('test_video')->count(); }); $this->assertEquals(5, $result); $result = Jam_Behavior_Paranoid::with_filter(Jam_Behavior_Paranoid::DELETED, function () { return Jam::all('test_video')->count(); }); $this->assertEquals(2, $result); $video = Jam_Behavior_Paranoid::with_filter(Jam_Behavior_Paranoid::ALL, function () { return Jam::all('test_video')->where(':primary_key', '=', 1)->first(); }); $video->restore_delete(); $this->assertCount(4, Jam::all('test_video')); $result = Jam_Behavior_Paranoid::with_filter(Jam_Behavior_Paranoid::ALL, function () { return Jam::all('test_video')->count(); }); $this->assertEquals(5, $result); $result = Jam_Behavior_Paranoid::with_filter(Jam_Behavior_Paranoid::DELETED, function () { return Jam::all('test_video')->count(); }); $this->assertEquals(1, $result); $video->real_delete(); $this->assertCount(3, Jam::all('test_video')); $result = Jam_Behavior_Paranoid::with_filter(Jam_Behavior_Paranoid::ALL, function () { return Jam::all('test_video')->count(); }); $this->assertEquals(4, $result); $result = Jam_Behavior_Paranoid::with_filter(Jam_Behavior_Paranoid::DELETED, function () { return Jam::all('test_video')->count(); }); $this->assertEquals(1, $result); }
/** * @covers Model_Brand_Refund_Item::purchase_item_price */ public function test_purchase_item_price() { $purchase_item = Jam::find('purchase_item', 1); $item = Jam::build('brand_refund_item', array('purchase_item' => $purchase_item)); $this->assertEquals($purchase_item->price(), $item->purchase_item_price()); }
public function test_skippable_field() { $store_purchase = Jam::find('test_store_purchase', 3); $store_purchase->freeze(); $this->assertNull($store_purchase->test_items[0]->price); $this->assertNotNull($store_purchase->test_items[1]->price); $store_purchase->test_items[0]->is_not_freezable = FALSE; $store_purchase->freeze(); $this->assertNotNull($store_purchase->test_items[0]->price); $this->assertNotNull($store_purchase->test_items[1]->price); $store_purchase->unfreeze(); $this->assertNull($store_purchase->test_items[0]->price); $this->assertNull($store_purchase->test_items[1]->price); $store_purchase->freeze(); $this->assertNotNull($store_purchase->test_items[0]->price); $this->assertNotNull($store_purchase->test_items[1]->price); }
/** * @covers Jam_Behavior_Shippable_Brand_Purchase::model_call_shipping_address */ public function test_model_call_shipping_address() { $address = Jam::find('address', 1); $purchase = $this->getMock('Model_Purchase', array('shipping_address'), array('model_purchase')); $purchase->expects($this->once())->method('shipping_address')->will($this->returnValue($address)); $brand_purchase = Jam::build('brand_purchase', array('purchase' => $purchase)); $this->assertSame($address, $brand_purchase->shipping_address()); }
/** * A widget to enter a belnogsto / hasone like association. * For displaying an exisiting item, you can use a template or a url. * If you use a template, it gets 'model', 'name', 'item' and 'id' as variables inside of it. * If you use a 'url' option - it will use its response instead of a template, passing 'model', 'id' and 'name' as query parameter fillers. E.g. /admin/images/build?model={{model}}&id={{id}} * * Options: * - model: string, defaults to the foreign_model of the association, can be comma separated * - template: string, the path for the view that is used to render an existing item * - url: string, a url to render an existing item * - container: the html id of the container tag * - source: string, the url used to retrieve the typeahead data. Defaults to the builtin typeahead action * - placeholder: the placeholder for the typeahead search input * * @param string $name * @param array $options * @param array $attributes * @return string */ public function remoteselect($name, array $options = array(), array $attributes = array()) { $attributes = $this->default_attributes($name, $attributes); return Tart::html($this, function ($h, $self) use($name, $options, $attributes) { $current = $self->object()->{$name}; $disabled = Arr::get($attributes, 'disabled', null); $model = isset($options['model']) ? $options['model'] : $self->object()->meta()->association($name)->foreign_model; $template = Arr::get($options, 'template', 'tart/typeahead/remoteselect'); if (!is_object($current) and $current) { $current = Jam::find($model, $current); } $options = Arr::merge(array('model' => $model, 'container' => $attributes['id'] . '_container', 'source' => Tart::uri('typeahead') . '?model=' . $model, 'url' => Tart::uri('typeahead', 'remoteselect_template') . '?model={{model}}&name={{name}}&id={{id}}&template=' . $template, 'template' => $template), $options); $options['url'] = strtr($options['url'], array('{{name}}' => $attributes['name'])); $h('input', array('name' => $attributes['name'], 'type' => 'hidden', 'value' => '', 'disabled' => $disabled)); $label = Arr::get($options, 'label', __(Inflector::humanize($name))); $default_placeholder = __('Search for :label', array(':label' => strtolower($label))); $h('input', array('type' => 'text', 'placeholder' => Arr::get($options, 'placeholder', $default_placeholder), 'class' => $current ? 'fade hide' : 'fade in', 'data-provide' => 'remoteselect', 'data-source' => $options['source'], 'data-container' => '#' . $options['container'], 'data-overwrite' => 1, 'data-url' => $options['url'], 'disabled' => $disabled)); $h('span', array('id' => $options['container'], 'class' => 'remoteselect-container'), function ($h, $self) use($current, $model, $options) { if ($current) { $url = strtr($options['url'], array('{{model}}' => $model, '{{id}}' => Jam_Form::list_id($current))); $split_uri = explode('?', $url); $uri = array_shift($split_uri); $query = array(); if ($split_uri) { parse_str($split_uri[0], $query); } $h->add(Request::factory($uri)->query($query)->execute()); } }); })->render(); }
/** * Get an item based on a unique key from the database * @param string $foreign_model * @param string $key * @return Jam_Model */ protected function _find_item($foreign_model, $key) { if (!$foreign_model) { return NULL; } return Jam::find($foreign_model, $key); }
public function test_complex_association() { $purchase = Jam::find('purchase', 2); $purchase->shipping_country(Jam::find('location', 'France')); $new_product = Jam::find('product', 3); $new_product2 = Jam::find('product', 4); $purchase->add_item($new_product->brand, Jam::build('purchase_item_product', array('is_payable' => TRUE, 'reference' => $new_product))); $purchase->add_item($new_product2->brand, Jam::build('purchase_item_product', array('is_payable' => TRUE, 'reference' => $new_product2))); $purchase->update_items()->freeze()->save(); $result = Jam::find('purchase', 2); $this->assertEquals($result->brand_purchases[0]->items[3]->reference->id(), $purchase->brand_purchases[0]->shipping->id()); $this->assertEquals($result->brand_purchases[0]->items[2]->id(), $purchase->brand_purchases[0]->shipping->items[1]->purchase_item->id()); }