Skip to content

Commit

Permalink
#211 - fixed the article attachment upload/delete functionality and r…
Browse files Browse the repository at this point in the history
…elated unit tests
  • Loading branch information
alphadevx committed Sep 14, 2015
1 parent 70f8fbc commit 2a1bf69
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 24 deletions.
17 changes: 12 additions & 5 deletions Alpha/Controller/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,17 @@ public function doPUT($request)
$viewState->set('markdownTextBoxRows', $params['markdownTextBoxRows']);
}

if (isset($params['ActiveRecordOID'])) {
if (isset($params['title']) || isset($params['ActiveRecordOID'])) {

$record = new Article();
$record->load($params['ActiveRecordOID']);

if (isset($params['title'])) {
$title = str_replace($config->get('cms.url.title.separator'), ' ', $params['title']);

$record->loadByAttribute('title', $title, false, array('OID', 'version_num', 'created_ts', 'updated_ts', 'title', 'author', 'published', 'content', 'headerContent'));
} else {
$record->load($params['ActiveRecordOID']);
}

// uploading an article attachment
if (isset($params['uploadBut'])) {
Expand All @@ -366,7 +373,7 @@ public function doPUT($request)
self::$logger->action('File '.$source.' uploaded to '.$dest);
$this->setStatusMessage(View::displayUpdateMessage('File '.$source.' uploaded to '.$dest));
}
} elseif (isset($params['deletefile'])) {
} elseif (isset($params['deletefile']) && $params['deletefile'] != '') {
$success = unlink($record->getAttachmentsLocation().'/'.$params['deletefile']);

if (!$success) {
Expand Down Expand Up @@ -404,9 +411,9 @@ public function doPUT($request)
$response->redirect($this->getNextJob());
} else {
if ($this->request->isSecureURI()) {
$response->redirect(FrontController::generateSecureURL('act=Alpha\\Controller\\ActiveRecordController&ActiveRecordType='.$ActiveRecordType.'&ActiveRecordOID='.$record->getOID().'&view=edit'));
$response->redirect(FrontController::generateSecureURL('act=Alpha\\Controller\\ActiveRecordController&ActiveRecordType=Alpha\Model\Article&ActiveRecordOID='.$record->getOID().'&view=edit'));
} else {
$response->redirect($config->get('app.url').'/record/'.$params['ActiveRecordType'].'/'.$record->getOID().'/edit');
$response->redirect($config->get('app.url').'/a/'.$params['title'].'/edit');
}
}

Expand Down
8 changes: 4 additions & 4 deletions Alpha/View/ArticleView.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function editView($fields = array())
$fields['formAction'] = $fields['URI'];

// the form ID
$fields['formID'] = get_class($this->BO).'_'.$this->BO->getID();
$fields['formID'] = stripslashes(get_class($this->BO)).'_'.$this->BO->getID();

// buffer form fields to $formFields
$fields['formFields'] = $this->renderAllFields('edit');
Expand Down Expand Up @@ -238,8 +238,8 @@ protected function renderFileUploadSection()
label: 'Okay',
cssClass: 'btn btn-default btn-xs',
action: function(dialogItself) {
$('[id=\"".($config->get('security.encrypt.http.fieldnames') ? base64_encode(SecurityUtils::encrypt('file_to_delete')) : 'file_to_delete')."\"]').attr('value', '".$file."');
$('#".get_class($this->BO).'_'.$this->BO->getID()."').submit();
$('[id=\"".($config->get('security.encrypt.http.fieldnames') ? base64_encode(SecurityUtils::encrypt('deletefile')) : 'deletefile')."\"]').attr('value', '".$file."');
$('[id=\"".stripslashes(get_class($this->BO)).'_'.$this->BO->getID()."\"]').submit();
dialogItself.close();
}
}
Expand All @@ -266,7 +266,7 @@ protected function renderFileUploadSection()
$temp = new Button('submit', 'Upload', 'uploadBut');
$html .= $temp->render();

$fieldname = ($config->get('security.encrypt.http.fieldnames') ? base64_encode(SecurityUtils::encrypt('file_to_delete')) : 'file_to_delete');
$fieldname = ($config->get('security.encrypt.http.fieldnames') ? base64_encode(SecurityUtils::encrypt('deletefile')) : 'deletefile');
$html .= '<input type="hidden" name="'.$fieldname.'" id="'.$fieldname.'" value=""/>';

$html .= '</div>';
Expand Down
4 changes: 2 additions & 2 deletions Alpha/View/Renderer/Html/RendererProviderHTML.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public function createView($fields = array())
$config = ConfigProvider::getInstance();

// the form ID
$fields['formID'] = get_class($this->BO).'_'.$this->BO->getOID();
$fields['formID'] = stripslashes(get_class($this->BO).'_'.$this->BO->getOID());

// buffer form fields to $formFields
$fields['formFields'] = $this->renderAllFields('create');
Expand Down Expand Up @@ -148,7 +148,7 @@ public function editView($fields = array())
$config = ConfigProvider::getInstance();

// the form ID
$fields['formID'] = get_class($this->BO).'_'.$this->BO->getOID();
$fields['formID'] = stripslashes(get_class($this->BO).'_'.$this->BO->getOID());

// buffer form fields to $formFields
$fields['formFields'] = $this->renderAllFields('edit');
Expand Down
15 changes: 4 additions & 11 deletions test/Alpha/Test/Controller/ArticleControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,15 +221,6 @@ public function testDoPUT()

$securityParams = $controller->generateSecurityFields();

$params = array('saveBut' => true, 'var1' => $securityParams[0], 'var2' => $securityParams[1]);
$params = array_merge($params, $article->toArray());

$request = new Request(array('method' => 'PUT', 'URI' => '/a/test-article', 'params' => $params));

$response = $front->process($request);

$this->assertEquals(200, $response->getStatus(), 'Testing the doPUT method');

$attachment = array(
'name' => 'logo.png',
'type' => 'image/png',
Expand All @@ -243,7 +234,8 @@ public function testDoPUT()

$response = $front->process($request);

$this->assertEquals(200, $response->getStatus(), 'Testing the doPUT method');
$this->assertEquals(301, $response->getStatus(), 'Testing the doPUT method');
$this->assertTrue(strpos($response->getHeader('Location'), '/a/test-article/edit') !== false, 'Testing the doPUT method');
$this->assertTrue(file_exists($article->getAttachmentsLocation().'/logo.png'));

$params = array('deletefile' => 'logo.png', 'var1' => $securityParams[0], 'var2' => $securityParams[1]);
Expand All @@ -253,7 +245,8 @@ public function testDoPUT()

$response = $front->process($request);

$this->assertEquals(200, $response->getStatus(), 'Testing the doPUT method');
$this->assertEquals(301, $response->getStatus(), 'Testing the doPUT method');
$this->assertTrue(strpos($response->getHeader('Location'), '/a/test-article/edit') !== false, 'Testing the doPUT method');
$this->assertFalse(file_exists($article->getAttachmentsLocation().'/logo.png'));
}
}
6 changes: 4 additions & 2 deletions test/Alpha/Test/Controller/AttachmentControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ public function testDoGET()

$response = $front->process($request);

$this->assertEquals(200, $response->getStatus(), 'Testing the doPUT method');
$this->assertEquals(301, $response->getStatus(), 'Testing the doGET method');
$this->assertTrue(strpos($response->getHeader('Location'), '/a/test-article/edit') !== false, 'Testing the doGET method');
$this->assertTrue(file_exists($article->getAttachmentsLocation().'/logo.png'));

$controller = new AttachmentController();
Expand All @@ -192,7 +193,8 @@ public function testDoGET()

$response = $front->process($request);

$this->assertEquals(200, $response->getStatus(), 'Testing the doPUT method');
$this->assertEquals(301, $response->getStatus(), 'Testing the doGET method');
$this->assertTrue(strpos($response->getHeader('Location'), '/a/test-article/edit') !== false, 'Testing the doGET method');
$this->assertFalse(file_exists($article->getAttachmentsLocation().'/logo.png'));
}
}

0 comments on commit 2a1bf69

Please sign in to comment.