public function testConfigSet()
 {
     $behavior = new Behavior(['model' => 'li3_behaviors\\tests\\mocks\\data\\model\\MockPost', 'test' => 'case']);
     $behavior->config('test', 'phase');
     $expected = 'phase';
     $result = $behavior->config('test');
     $this->assertEqual($expected, $result);
 }
Example #2
0
 public function testConfig()
 {
     $behavior = new Behavior(['test1' => 'value1']);
     $this->assertEqual('value1', $behavior->config('test1'));
     $behavior->config(['test2' => 'value2']);
     $this->assertEqual('value1', $behavior->config('test1'));
     $this->assertEqual('value2', $behavior->config('test2'));
     $behavior->config(['test1' => 'value1 changed', 'test2' => 'value2']);
     $this->assertEqual('value1 changed', $behavior->config('test1'));
     $this->assertEqual('value2', $behavior->config('test2'));
 }
 protected static function _validates($model, Behavior $behavior)
 {
     $model::applyFilter('validates', function ($self, $params, $chain) use($behavior) {
         $entity =& $params['entity'];
         if (!$entity->i18n) {
             // When no i18n is present, we don't have to do anything.
             return $chain->next($self, $params, $chain);
         }
         $config = $behavior->config();
         $entity = static::_syncFromI18n($entity, $config);
         if ($diff = array_diff(array_keys($entity->i18n), $config['fields'])) {
             "Unknown translated field/s `" . implode(', ', $diff);
             "`.";
             throw new Exception($message);
         }
         // Validate original fields, as well as any translation
         // that are present. By default translation are sparse
         // and cannot be *required*.
         $rules =& $params['options']['rules'];
         foreach ($config['fields'] as $field) {
             if (!isset($rules[$field])) {
                 continue;
             }
             foreach ($config['locales'] as $locale) {
                 if ($locale === $config['locale']) {
                     continue;
                 }
                 $inline = static::_composeField($field, $locale, '.');
                 if (isset($rules[$inline])) {
                     continue;
                 }
                 $rules[$inline] = $rules[$field];
             }
         }
         foreach ($rules as $field => $rule) {
             if (strpos($field, 'i18n') === false) {
                 continue;
             }
             foreach ($rule as &$r) {
                 $r['required'] = false;
             }
         }
         return $chain->next($self, $params, $chain);
     });
 }
Example #4
0
 /**
  * Adds the `tag` finder. Implements custom logic for SQL
  * based data sources otherwise a pass through.
  *
  * @deprecated
  * @param string $model Class name of the model.
  * @param object $behavior Instance of the behavior.
  * @return void
  */
 protected static function _finders($model, Behavior $behavior)
 {
     $connection = get_class($model::connection());
     $model::finder('tag', function ($self, $params, $chain) use($behavior, $connection) {
         trigger_error('The `tag` finder is deprecated, use regular finders instead.', E_USER_DEPRECATED);
         $field = $behavior->config('field');
         if (!$connection::enabled('arrays')) {
             $params['options']['conditions'][$field] = ['REGEXP' => '(,|^)' . $params['options']['conditions'][$field] . '(,|$)'];
         }
         return $chain->next($self, $params, $chain);
     });
 }