spatie / laravel-activitylog

Log activity inside your Laravel app
https://docs.spatie.be/laravel-activitylog
MIT License
5.36k stars 714 forks source link

useAttributeRawValues ignores attribute mutation #1186

Closed debuqer closed 1 year ago

debuqer commented 1 year ago

Describe the bug As far as i know, attributesToBeLogged always contains mutated data, but when ActivityLog suppose to ignore casts, using useAttributeRawValues(), mutation method will ignore

To Reproduce

it('can ignore cast and store mutated value while attribute has mutator', function () {
    $articleClass = new class() extends Article {
        use LogsActivity;

        protected $casts = [
            'status' => 'string',
        ];

        public function getStatusAttribute($value)
        {
            return $value ? 'Show' : 'Hide';
        }

        public function getActivitylogOptions(): LogOptions
        {
            return LogOptions::defaults()->logOnly(['status'])->useAttributeRawValues(['status']);
        }
    };

    $article = new $articleClass();
    $article->status = 1;
    $article->save();

    $this->assertEquals($article->status, $this->getLastActivity()->properties['attributes']['status']); // expected 'Show' but '1' given
});

If you use useAttributeRawValues(['status'])

"attributes" =>  [
      "status" => 1
    ]

If you use useAttributeRawValues([])

"attributes" =>  [
      "status" => "Show"
    ]

Versions

I'm not sure if its correct behavior or not, but based on documentation useAttributeRawValues let ActivityLog to store the data without casting and it's just not about mutation.

spatie-bot commented 1 year ago

Dear contributor,

because this issue seems to be inactive for quite some time now, I've automatically closed it. If you feel this issue deserves some attention from my human colleagues feel free to reopen it.