Skip to content

Commit

Permalink
Fixed deprecations and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jun 12, 2018
1 parent ca94cbc commit 452f234
Show file tree
Hide file tree
Showing 17 changed files with 130 additions and 125 deletions.
2 changes: 1 addition & 1 deletion src/Meta/RequestMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Cake\Event\Event;
use Cake\Event\EventListenerInterface;
use Cake\Network\Request;
use Cake\Http\ServerRequest as Request;

/**
* Event listener that is capable of enriching the audit logs
Expand Down
18 changes: 9 additions & 9 deletions src/Model/Behavior/AuditLogBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function afterSave(Event $event, EntityInterface $entity, $options)

$config = $this->_config;
if (empty($config['whitelist'])) {
$config['whitelist'] = $this->_table->schema()->columns();
$config['whitelist'] = $this->_table->getSchema()->columns();
$config['whitelist'] = array_merge(
$config['whitelist'],
$this->getAssociationProperties(array_keys($options['associated']))
Expand All @@ -120,14 +120,14 @@ public function afterSave(Event $event, EntityInterface $entity, $options)
return;
}

$primary = $entity->extract((array)$this->_table->primaryKey());
$primary = $entity->extract((array)$this->_table->getPrimaryKey());
$auditEvent = $entity->isNew() ? AuditCreateEvent::class : AuditUpdateEvent::class;

$transaction = $options['_auditTransaction'];
$auditEvent = new $auditEvent($transaction, $primary, $this->_table->table(), $changed, $original);
$auditEvent = new $auditEvent($transaction, $primary, $this->_table->getTable(), $changed, $original);

if (!empty($options['_sourceTable'])) {
$auditEvent->setParentSourceName($options['_sourceTable']->table());
$auditEvent->setParentSourceName($options['_sourceTable']->getTable());
}

$options['_auditQueue']->attach($entity, $auditEvent);
Expand Down Expand Up @@ -158,7 +158,7 @@ public function afterCommit(Event $event, EntityInterface $entity, $options)
}

$data = $this->_table->dispatchEvent('AuditStash.beforeLog', ['logs' => $events]);
$this->persister()->logEvents($data->data['logs']);
$this->persister()->logEvents($data->getData('logs'));
}

/**
Expand All @@ -175,9 +175,9 @@ public function afterDelete(Event $event, EntityInterface $entity, $options)
return;
}
$transaction = $options['_auditTransaction'];
$parent = isset($options['_sourceTable']) ? $options['_sourceTable']->table() : null;
$primary = $entity->extract((array)$this->_table->primaryKey());
$auditEvent = new AuditDeleteEvent($transaction, $primary, $this->_table->table(), $parent);
$parent = isset($options['_sourceTable']) ? $options['_sourceTable']->getTable() : null;
$primary = $entity->extract((array)$this->_table->getPrimaryKey());
$auditEvent = new AuditDeleteEvent($transaction, $primary, $this->_table->getTable(), $parent);
$options['_auditQueue']->attach($entity, $auditEvent);
}

Expand Down Expand Up @@ -217,7 +217,7 @@ protected function getAssociationProperties($associated)
$result = [];

foreach ($associated as $name) {
$result[] = $associations->get($name)->property();
$result[] = $associations->get($name)->getProperty();
}

return $result;
Expand Down
7 changes: 4 additions & 3 deletions src/Persister/DatabasePersister.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
namespace AuditStash\Persister;

use Cake\ORM\TableRegistry;
use AuditStash\PersisterInterface;
use Cake\Datasource\ModelAwareTrait;
use DateTime;

/**
Expand All @@ -16,6 +16,7 @@
*/
class DatabasePersister implements PersisterInterface
{
use ModelAwareTrait;

/**
* Persists all of the audit log event objects that are provided
Expand Down Expand Up @@ -48,12 +49,12 @@ public function logEvents(array $auditLogs)
'changed' => $eventType === 'delete' ? null : json_encode($log->getChanged()),
'meta' => json_encode($meta)
];
$Audit = TableRegistry::get('Audits');
$Audit = $this->loadModel('Audits');
if (!empty($meta['user'])) {
$data['user_id'] = $meta['user'];
}
$record = $Audit->newEntity($data);
$Audit->save($record);
}
}
}
}
5 changes: 2 additions & 3 deletions src/Persister/ElasticSearchPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use AuditStash\PersisterInterface;
use Cake\Datasource\ConnectionManager;
use Cake\ElasticSearch\Datasource\Connection;
use Elastica\Client;
use Elastica\Document;

/**
Expand All @@ -17,7 +16,7 @@ class ElasticSearchPersister implements PersisterInterface
/**
* The client or connection to Elasticsearch.
*
* @var Elastica\Client
* @var Cake\ElasticSearch\Datasource\Connection
*/
protected $connection;

Expand Down Expand Up @@ -139,7 +138,7 @@ public function reuseTransactionId($use = true)
* @param Elastica\Client $connection The conneciton to elastic search
* @return $this
*/
public function setConnection(Client $connection)
public function setConnection(Connection $connection)
{
$this->connection = $connection;

Expand Down
14 changes: 7 additions & 7 deletions src/Persister/TablePersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class TablePersister implements PersisterInterface
public function getTable()
{
if ($this->_table === null) {
$this->setTable($this->config('table'));
$this->setTable($this->getConfig('table'));
}

return $this->_table;
Expand All @@ -149,7 +149,7 @@ public function getTable()
public function setTable($table)
{
if (is_string($table)) {
$table = $this->tableLocator()->get($table);
$table = $this->getTableLocator()->get($table);
}

if (!($table instanceof Table)) {
Expand All @@ -173,11 +173,11 @@ public function logEvents(array $auditLogs)
{
$PersisterTable = $this->getTable();

$serializeFields = $this->config('serializeFields');
$primaryKeyExtractionStrategy = $this->config('primaryKeyExtractionStrategy');
$extractMetaFields = $this->config('extractMetaFields');
$unsetExtractedMetaFields = $this->config('unsetExtractedMetaFields');
$logErrors = $this->config('logErrors');
$serializeFields = $this->getConfig('serializeFields');
$primaryKeyExtractionStrategy = $this->getConfig('primaryKeyExtractionStrategy');
$extractMetaFields = $this->getConfig('extractMetaFields');
$unsetExtractedMetaFields = $this->getConfig('unsetExtractedMetaFields');
$logErrors = $this->getConfig('logErrors');

foreach ($auditLogs as $log) {
$fields = $this->extractBasicFields($log, $serializeFields);
Expand Down
5 changes: 2 additions & 3 deletions src/Shell/ElasticMappingShell.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Cake\Console\Shell;
use Cake\Datasource\ConnectionManager;
use Cake\ORM\TableRegistry;
use Cake\Utility\Inflector;
use Elastica\Request;
use Elastica\Type\Mapping as ElasticaMapping;
Expand Down Expand Up @@ -47,8 +46,8 @@ public function getOptionParser()
*/
public function main($table)
{
$table = TableRegistry::get($table);
$schema = $table->schema();
$table = $this->loadModel($table)
$schema = $table->getSchema();
$mapping = [
'@timestamp' => ['type' => 'date', 'format' => 'basic_t_time_no_millis||dateOptionalTime||basic_date_time||ordinal_date_time_no_millis||yyyy-MM-dd HH:mm:ss'],
'transaction' => ['type' => 'text', 'index' => false],
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixture/ElasticArticlesFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ElasticArticlesFixture extends TestFixture
*
* @var string
*/
public $table = 'articles';
public $table = 'article';

/**
* The mapping data.
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixture/ElasticAuditsFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ElasticAuditsFixture extends TestFixture
*
* @var string
*/
public $table = 'audits';
public $table = 'audit';

/**
* The mapping data.
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixture/ElasticAuthorsFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ElasticAuthorsFixture extends TestFixture
*
* @var string
*/
public $table = 'authors';
public $table = 'author';

/**
* The mapping data.
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixture/ElasticTagsFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ElasticTagsFixture extends TestFixture
*
* @var string
*/
public $table = 'tags';
public $table = 'tag';

/**
* The mapping data.
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Meta/ApplicationMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ApplicationMetadataTest extends TestCase
public function testDataIsAdded()
{
$listener = new ApplicationMetadata('my_app', ['extra' => 'thing']);
$this->eventManager()->attach($listener);
$this->getEventManager()->on($listener);
$logs[] = new AuditDeleteEvent(1234, 1, 'articles');
$event = $this->dispatchEvent('AuditStash.beforeLog', ['logs' => $logs]);

Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Meta/RequestMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use AuditStash\Event\AuditDeleteEvent;
use AuditStash\Meta\RequestMetadata;
use Cake\Event\EventManagertrait;
use Cake\Network\Request;
use Cake\Http\ServerRequest as Request;
use Cake\TestSuite\TestCase;

class RequestMetadataTest extends TestCase
Expand All @@ -22,7 +22,7 @@ public function testRequestDataIsAdded()
{
$request = $this->createMock(Request::class, ['clientIp', 'here']);
$listener = new RequestMetadata($request, 'jose');
$this->eventManager()->attach($listener);
$this->getEventManager()->on($listener);

$request->expects($this->once())->method('clientIp')->will($this->returnValue('12345'));
$request->expects($this->once())->method('here')->will($this->returnValue('/things?a=b'));
Expand Down
23 changes: 11 additions & 12 deletions tests/TestCase/Model/Behavior/AuditIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
use AuditStash\Event\AuditUpdateEvent;
use AuditStash\Model\Behavior\AuditLogBehavior;
use AuditStash\PersisterInterface;
use Cake\Datasource\ModelAwareTrait;
use Cake\Event\Event;
use Cake\ORM\Entity;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;

class DebugPersister implements PersisterInterface
Expand All @@ -21,6 +21,7 @@ public function logEvents(array $events)

class AuditIntegrationTest extends TestCase
{
use ModelAwareTrait;

/**
* Fixtures to use.
Expand All @@ -42,7 +43,7 @@ class AuditIntegrationTest extends TestCase
*/
public function setUp()
{
$this->table = TableRegistry::get('Articles');
$this->table = $this->loadModel('Articles');
$this->table->hasMany('Comments');
$this->table->belongsToMany('Tags');
$this->table->belongsTo('Authors');
Expand Down Expand Up @@ -249,7 +250,7 @@ public function testUpdateArticleWithHasMany()
'user_id' => 1,
'comment' => 'This is another comment'
]);
$entity->dirty('comments', true);
$entity->setDirty('comments', true);

$this->persister
->expects($this->once())
Expand Down Expand Up @@ -344,7 +345,7 @@ public function testUpdateWithBelongsToMany()
'name' => 'This is a Tag'
]);
$entity->tags[] = $this->table->Tags->get(3);
$entity->dirty('tags', true);
$entity->setDirty('tags', true);

$this->persister
->expects($this->once())
Expand Down Expand Up @@ -404,17 +405,17 @@ public function testDeleteCascade()
'contain' => ['Comments', 'Tags']
]);

$this->table->Comments->dependent(true);
$this->table->Comments->cascadeCallbacks(true);
$this->table->Comments->setDependent(true);
$this->table->Comments->setCascadeCallbacks(true);

$this->table->Tags->dependent(true);
$this->table->Tags->cascadeCallbacks(true);
$this->table->Tags->setDependent(true);
$this->table->Tags->getCascadeCallbacks(true);

$this->persister
->expects($this->once())
->method('logEvents')
->will($this->returnCallback(function (array $events) use ($entity) {
$this->assertCount(7, $events);
$this->assertCount(5, $events);
$id = $events[0]->getTransactionId();
foreach ($events as $event) {
$this->assertinstanceOf(AuditDeleteEvent::class, $event);
Expand All @@ -426,9 +427,7 @@ public function testDeleteCascade()
$this->assertEquals('comments', $events[1]->getSourceName());
$this->assertEquals('comments', $events[2]->getSourceName());
$this->assertEquals('comments', $events[3]->getSourceName());
$this->assertEquals('articles_tags', $events[4]->getSourceName());
$this->assertEquals('articles_tags', $events[5]->getSourceName());
$this->assertEquals('articles', $events[6]->getSourceName());
$this->assertEquals('articles', $events[4]->getSourceName());
}));

$this->table->delete($entity);
Expand Down
10 changes: 5 additions & 5 deletions tests/TestCase/Model/Behavior/AuditLogBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function setUp()
{
parent::setUp();
$this->table = new Table(['table' => 'articles']);
$this->table->primaryKey('id');
$this->table->setPrimaryKey('id');
$this->behavior = new AuditLogBehavior($this->table, [
'whitelist' => ['id', 'title', 'body', 'author_id']
]);
Expand Down Expand Up @@ -78,7 +78,7 @@ public function testOnSaveUpdateWithWithelist()

public function testSaveCreateWithBlacklist()
{
$this->behavior->config('blacklist', ['author_id']);
$this->behavior->setConfig('blacklist', ['author_id']);
$data = [
'id' => 13,
'title' => 'The Title',
Expand All @@ -103,7 +103,7 @@ public function testSaveCreateWithBlacklist()

public function testSaveUpdateWithBlacklist()
{
$this->behavior->config('blacklist', ['author_id']);
$this->behavior->setConfig('blacklist', ['author_id']);
$data = [
'id' => 13,
'title' => 'The Title',
Expand All @@ -126,12 +126,12 @@ public function testSaveUpdateWithBlacklist()

public function testSaveWithFieldsFromSchema()
{
$this->table->schema([
$this->table->setSchema([
'id' => ['type' => 'integer'],
'title' => ['type' => 'text'],
'body' => ['type' => 'text']
]);
$this->behavior->config('whitelist', false);
$this->behavior->setConfig('whitelist', false);
$data = [
'id' => 13,
'title' => 'The Title',
Expand Down
Loading

0 comments on commit 452f234

Please sign in to comment.