public function testThrowsExceptionOnInvalidCode()
 {
     $this->setExpectedException('\\InvalidArgumentException');
     $values = ['source' => '/test-3', 'contentId' => 1, 'contentType' => 'entries', 'code' => 200];
     $redirect = new Redirect($values);
     $redirect->save();
 }
 public function testDefaultCode()
 {
     $entry = new Content($this->app, 'entries');
     $entry->setValue('title', 'Test');
     $entry->setValue('slug', 'test-default-code');
     $entry->setValue('ownerid', 1);
     $entry->setValue('status', 'published');
     $id = $this->app['storage']->saveContent($entry);
     $values = ['source' => '/test-default-code', 'contentId' => $id, 'contentType' => 'entries', 'code' => null];
     $redirect = new Redirect($values);
     $redirect->save();
     $request = Request::create('/test-default-code');
     $response = $this->app->handle($request);
     $default = $this->extension->config['default_status_code'];
     $default = empty($default) ? 302 : $default;
     $this->assertEquals($response->getStatusCode(), $default);
 }
 protected function setup()
 {
     $this->app = $this->getApp();
     $this->extension = new Extension($this->app);
     $this->app['extensions']->register($this->extension);
     $this->extension->dbCheck();
     // Register the database table.
     $this->app['integritychecker']->repairTables();
     Redirect::$dbConnection = $this->app['db'];
     Redirect::$tableName = $this->extension->getTableName();
 }
 public function handleRequest(Request $request)
 {
     // Look for a migrated article with this URL path.
     $requested_path = $request->getPathInfo();
     if ($this->isRedirectable($requested_path) && ($redirect = Redirect::load($requested_path))) {
         $status_code = $redirect->code;
         $status_code = empty($status_code) ? $this->config['default_status_code'] : $status_code;
         if (!in_array($status_code, Redirect::$validCodes)) {
             $this->app['logger.system']->error("Prevented an invalid HTTP code ({$status_code}) from being sent for '{$requested_path}'. Instead, used 302 as fallback.", ['event' => 'contentredirect']);
             $status_code = 302;
         }
         $record = $this->app['storage']->getContent("{$redirect->contentType}/{$redirect->contentId}");
         if ($record) {
             $root_path = $this->app['paths']['root'];
             $record_path = substr($record->link(), 1);
             // Strip off the first '/'.
             return $this->app->redirect($root_path . $record_path, $status_code);
         } else {
             $this->app['logger.system']->error("Couldn't find content with type '{$content_type}' and id '{$content_id}'.", ['event' => 'contentredirect']);
             return false;
         }
     }
 }