FOF30\Model\DataModel\Behaviour\Assets::onBeforeDelete PHP Méthode

onBeforeDelete() public méthode

public onBeforeDelete ( DataModel &$model, $oid )
$model FOF30\Model\DataModel
    public function onBeforeDelete(DataModel &$model, $oid)
    {
        if (!$model->isAssetsTracked()) {
            return true;
        }
        $k = $model->getKeyName();
        // If the table is not loaded, let's try to load it with the id
        if (!$model->{$k}) {
            $model->load($oid);
        }
        // If I have an invalid assetName I have to stop
        $name = $model->getAssetName();
        // Do NOT touch JTable here -- we are loading the core asset table which is a JTable, not a FOF Table
        $asset = \JTable::getInstance('Asset');
        if ($asset->loadByName($name)) {
            // Since we are using JTable, there is no way to mock it and test for failures :(
            // @codeCoverageIgnoreStart
            if (!$asset->delete()) {
                throw new \Exception($asset->getError());
            }
            // @codeCoverageIgnoreEnd
        }
        return true;
    }

Usage Example

Exemple #1
0
 /**
  * @group           Behaviour
  * @group           AssetsOnBeforeDelete
  * @covers          FOF30\Model\DataModel\Behaviour\Assets::onBeforeDelete
  * @dataProvider    AssetsDataprovider::getTestOnBeforeDelete
  */
 public function testOnBeforeDelete($test, $check)
 {
     $msg = 'Own::onBeforeDelete %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 ($check['exception']) {
         $this->setExpectedException('FOF30\\Model\\DataModel\\Exception\\NoAssetKey');
     }
     $query = $db->getQuery(true)->select('COUNT(*)')->from('#__assets');
     $beforeTotal = $db->setQuery($query)->loadResult();
     $result = $behavior->onBeforeDelete($model, $test['id']);
     $this->assertTrue($result, sprintf($msg, 'Returned a wrong value'));
     $query = $db->getQuery(true)->select('COUNT(*)')->from('#__assets');
     $afterTotal = $db->setQuery($query)->loadResult();
     $this->assertEquals($check['count'], $beforeTotal - $afterTotal, sprintf($msg, 'Deleted a wrong number of assets'));
 }