Beispiel #1
0
 /**
  * @group           Behaviour
  * @group           AssetsOnAfterSave
  * @covers          FOF30\Model\DataModel\Behaviour\Assets::onAfterSave
  * @dataProvider    AssetsDataprovider::getTestOnAfterSave
  */
 public function testOnAfterSave($test, $check)
 {
     $msg = 'Own::onAfterBuildQuery %s - Case: ' . $check['case'];
     $db = \JFactory::getDbo();
     $config = array('idFieldName' => $test['tableid'], 'tableName' => $test['table']);
     $model = new DataModelStub(static::$container, $config);
     $dispatcher = $model->getBehavioursDispatcher();
     $behavior = new Assets($dispatcher);
     $model->setAssetsTracked($test['track']);
     if ($test['load']) {
         $model->find($test['load']);
     }
     if ($test['rules']) {
         $model->setRules($test['rules']);
     }
     $query = $db->getQuery(true)->select('COUNT(*)')->from('#__assets');
     $beforeTotal = $db->setQuery($query)->loadResult();
     $result = $behavior->onAfterSave($model);
     $this->assertTrue($result, sprintf($msg, 'Should always return true'));
     $asset = null;
     if ($check['count'] == 0) {
         $query = $db->getQuery(true)->select('COUNT(*)')->from('#__assets');
         $afterTotal = $db->setQuery($query)->loadResult();
         $this->assertEquals(0, $beforeTotal - $afterTotal, sprintf($msg, 'Wrong number of assets saved'));
     } else {
         // Let's check what has been saved
         $query = $db->getQuery(true)->select('id, rules')->from('#__assets')->where('name = ' . $db->q($model->getAssetName()));
         $asset = $db->setQuery($query)->loadObject();
         $this->assertEquals($check['count'], (int) (!is_null($asset)), sprintf('Wrong number of assets saved'));
     }
     if (!is_null($check['rules'])) {
         $this->assertEquals($check['rules'], $asset->rules, sprintf($msg, 'Wrong rule stored'));
     }
     if ($asset) {
         $asset_field = $model->getFieldAlias('asset_id');
         $model->find($test['load']);
         $this->assertEquals($asset->id, $model->{$asset_field}, sprintf($msg, 'Asset id not stored inside the model'));
     }
 }