/**
  * @dataProvider providerLogLegalTypeEvents
  */
 public function testLogLegalTypeEvents($event_type, $event_data, $user_id)
 {
     $tracker = $this->_buildFunctioningTracker(false);
     EventTrackerEvent::deleteAll([]);
     $tracker->logEvent($event_type, $event_data, $user_id);
     $logged_event = EventTrackerEvent::findOne(['event_type' => $event_type, 'user_id' => $user_id]);
     $this->assertTrue($logged_event instanceof EventTrackerEvent);
     $this->assertSame($event_data, json_decode($logged_event->event_data, true));
 }
 /**
  * Log an event. Optionally specify event data and/or a user ID. If you do not specify the user ID, the event will
  * be logged for the currently active user.
  *
  * If you have handlers that hook into the logging process, you can explicitly enable/disable these handlers by
  * using the $run_handlers parameter of this function.
  *
  * @param integer  $event_type   The event type specified by its ID. It is recommended to set this using a class
  * constant from your EventTypes object.
  * @param mixed    $event_data   Any event data that should be added and can be encoded into JSON format. Can be
  * NULL in which case no data will be added.
  * @param int|null $user_id      Optionally specify a user for which to log the event. If NULL the currently
  * logged in user will be used.
  * @param bool     $run_handlers Optionally specify whether or not to run post event handlers (If any are
  * configured). Defaults to TRUE.
  * @return bool Whether the event was successfully logged.
  * @throws InvalidParamException Whenever the event data could not be encoded into JSON format or event_type is not
  * a valid event ID.
  * @throws Exception Whenever no user_id is given and there is no authenticated or existing user.
  * @throws IntegrityException Whenever the event could not be inserted into the database.
  */
 public function logEvent($event_type, $event_data = null, $user_id = null, bool $run_handlers = true) : bool
 {
     if (null !== $event_data) {
         $event_data = json_encode($event_data);
         if (false === $event_data) {
             throw new InvalidParamException('The event data could not be encoded into JSON format.');
         }
     }
     if (!is_integer($event_type) || !in_array($event_type, $this->eventTypesAvailable())) {
         throw new InvalidParamException('The event type ID is invalid.');
     }
     $event = new EventTrackerEvent(['timestamp' => $this->_trackerTime(), 'event_data' => $event_data, 'event_type' => $event_type]);
     if (null !== $user_id && is_integer($user_id)) {
         $event->user_id = $user_id;
     } else {
         $user = Yii::$app->get('user', false);
         if (null === $user || $user->isGuest) {
             throw new Exception('Cannot log event for non-existing or non-authenticated user.');
         }
         $event->user_id = $user->id;
     }
     if ($event->save()) {
         if (true === $run_handlers && $this->_post_event_handler instanceof PostEventInterface) {
             $this->_post_event_handler->afterLogEvent($event);
         }
         return true;
     } else {
         return false;
     }
 }
 public function testRulesMatch()
 {
     $event = new EventTrackerEvent();
     $this->assertSame([[['timestamp', 'user_id', 'event_type'], 'required'], [['timestamp', 'user_id', 'event_type'], 'integer'], ['event_data', 'safe']], $event->rules());
 }