Gabriyel Goede пре 3 недеља
родитељ
комит
6e48cbf0fc

+ 53 - 21
src-php/app/internship/controller/ArticleApiController.php

@@ -120,48 +120,80 @@ class ArticleApiController extends ControllerAdapter {
 	 * @throws StatusException
 	 */
 	function badRequestExceptionIfs(ParamBody $body, ?Article $article = null): Article {
+
 		$bodyData = $body->parseJson();
+		
 		if($body->getValue() === "") {
 			throw new BadRequestException('Body cant be empty');
 		}
-		$pattern = '/^{/';
-		$pattern2 = '/}$/';
 
-		if (preg_match($pattern2, $body) == 0 || preg_match($pattern, $body) == 0) {
+		$bracketStart = '/^{/';
+		$bracketEnd = '/}$/';
+		$spaceStart = '/^\s/';
+		$spaceEnd = '/\s$/';
+
+		if (preg_match($bracketEnd, $body) == 0 || preg_match($bracketStart, $body) == 0) {
 			throw new BadRequestException('Json is invalid');
 		}
 
-		if (!isset($bodyData['title'])) {
-			throw new BadRequestException('Title must be set');
-		}
+		if ($article === null) {
 
-		if (strlen($bodyData['title']) > 255) {
-			throw new BadRequestException('Title is too long');
-		}
+			if (!isset($bodyData['title'])) {
+				throw new BadRequestException('Title must be set');
+			}
 
-		if (!isset($bodyData['categoryName'])) {
-			throw new BadRequestException('The Category must be international, national or sport');
+			if (!isset($bodyData['categoryName'])) {
+				throw new BadRequestException('The Category must be international, national or sport');
+			}
+
+			if(!isset($bodyData['text'])) {
+				throw new BadRequestException('Article must contain a text');
+			}
 		}
+		if (isset($bodyData['text'])) {
+			if (strlen($bodyData['text']) > 12000) {
+				throw new BadRequestException('Text is too long');
+			}
 
-		if(!in_array($bodyData['categoryName'], ['sport','international','national'])) {
-			throw new BadRequestException('The Category must be international, national or sport');
+			if (strlen($bodyData['text']) == 0) {
+				throw new BadRequestException('Text is too short');
+			}
+
+			if (preg_match($spaceStart, $bodyData['text']) == 1 || preg_match($spaceEnd, $bodyData['text']) == 1) {
+				throw new BadRequestException('Text cant start or end with a whitespace character');
+			}
 		}
+		if (isset($bodyData['title'])) {
+			if (strlen($bodyData['title']) > 255) {
+				throw new BadRequestException('Title is too long');
+			}
+			
+			if (strlen($bodyData['title']) == 0) {
+				throw new BadRequestException('Title is too short');
+			}
 
-		if(!isset($bodyData['text'])) {
-			throw new BadRequestException('Article must contain a text');
+			if (preg_match($spaceStart, $bodyData['title']) == 1 || preg_match($spaceEnd, $bodyData['title']) == 1) {
+				throw new BadRequestException('Title cant start or end with a whitespace character');
+			}
 		}
 
-		if (strlen($bodyData['text']) > 12000){
-			throw new BadRequestException('Text is too long');
+		if (isset($bodyData['categoryName']) && !in_array($bodyData['categoryName'], ['sport','international','national'])) {
+			throw new BadRequestException('The Category must be international, national or sport');
 		}
 
+
 		if ($article === null) {
 			$article = new Article();
 		}
-
-		$article->setCategoryName($bodyData['categoryName']);
-		$article->setText($bodyData['text']);
-		$article->setTitle($bodyData['title']);
+		if (isset($bodyData['categoryName'])) {
+			$article->setCategoryName($bodyData['categoryName']);
+		}
+		if (isset($bodyData['text'])) {
+			$article->setText($bodyData['text']);
+		}
+		if (isset($bodyData['title'])) {
+			$article->setTitle($bodyData['title']);
+		}
 
 		return $article;
 	}

+ 3 - 2
src-php/app/internship/model/ArticleDao.php

@@ -8,6 +8,7 @@ use n2n\persistence\Pdo;
 use n2n\web\http\BadRequestException;
 use n2n\web\http\controller\ParamBody;
 use n2n\web\http\PageNotFoundException;
+use n2n\web\http\StatusException;
 
 
 /**
@@ -55,8 +56,8 @@ class ArticleDao {
 	function getArticleById(mixed $id): ?Article {
 		return $this->em->find(Article::getClass(), $id);
 
-		$criteria = $this->em->createSimpleCriteria(Article::getClass(),['id'=> $id]);
-		return $criteria->toQuery()->fetchSingle();
+//		$criteria = $this->em->createSimpleCriteria(Article::getClass(),['id'=> $id]);
+//		return $criteria->toQuery()->fetchSingle();
 	}
 	
 	function saveArticle(Article $article) {

+ 48 - 48
src-php/test/internship/controller/ArticleControllerTest.php

@@ -40,23 +40,23 @@ class ArticleControllerTest extends TestCase {
 				->get(['api', 'articles'])
 				->exec();
 
-		$articleStruct = $response->parseJson();
-		$this->assertCount(3, $articleStruct);
+		$articleStructs = $response->parseJson();
+		$this->assertCount(3, $articleStructs);
 
-		$this->assertEquals('Title 3', $articleStruct[0]['title']);
-		$this->assertEquals('Title 2', $articleStruct[1]['title']);
-		$this->assertEquals('Title 1', $articleStruct[2]['title']);
+		$this->assertEquals('Title 3', $articleStructs[0]['title']);
+		$this->assertEquals('Title 2', $articleStructs[1]['title']);
+		$this->assertEquals('Title 1', $articleStructs[2]['title']);
 	}
 
 	function testGetDoArticlesWithCategory() {
 		$response = TestEnv::http()->newRequest()
-				->get(['api', 'articles', 'teaser'])
+				->get(['api', 'articles', 'news'])
 				->exec();
 
-		$articleStruct = $response->parseJson();
-		$this->assertCount(1, $articleStruct);
+		$articleStructs = $response->parseJson();
+		$this->assertCount(2, $articleStructs);
 
-		$this->assertEquals('Title 2', $articleStruct[0]['title']);
+		$this->assertEquals('Title 2', $articleStructs[0]['title']);
 	}
 
 	function testGetDoArticlesWithCategory2() {
@@ -64,11 +64,11 @@ class ArticleControllerTest extends TestCase {
 				->get(['api', 'articles', 'news'])
 				->exec();
 
-		$articleStruct = $response->parseJson();
-		$this->assertCount(2, $articleStruct);
+		$articleStructs = $response->parseJson();
+		$this->assertCount(2, $articleStructs);
 
-		$this->assertEquals('Title 3', $articleStruct[0]['title']);
-		$this->assertEquals('Title 1', $articleStruct[1]['title']);
+		$this->assertEquals('Title 3', $articleStructs[0]['title']);
+		$this->assertEquals('Title 1', $articleStructs[1]['title']);
 	}
 
 	function testGetDoArticlesExpectExceptionBecauseCategoryNotFound() {
@@ -85,12 +85,12 @@ class ArticleControllerTest extends TestCase {
 				->get(['api', 'article', $this->article1Id])
 				->exec();
 
-		$articleStruct = $response->parseJson();
-		$this->assertCount(4, $articleStruct);
+		$articleStructs = $response->parseJson();
+		$this->assertCount(4, $articleStructs);
 
-		$this->assertEquals('Title 1', $articleStruct['title']);
-		$this->assertEquals('Loren ipsum 1', $articleStruct['text']);
-		$this->assertEquals('news', $articleStruct['categoryName']);
+		$this->assertEquals('Title 1', $articleStructs['title']);
+		$this->assertEquals('Loren ipsum 1', $articleStructs['text']);
+		$this->assertEquals('news', $articleStructs['categoryName']);
 
 	}
 
@@ -118,13 +118,13 @@ class ArticleControllerTest extends TestCase {
             		'text' => 'text1'
        			])
 				->exec();
-		$articleStruct = $response->parseJson();
+		$articleStructs = $response->parseJson();
 		$this->assertEquals(4,TestEnv::emUtil()->count(Article::class));
-		$this->assertCount(4, $articleStruct);
+		$this->assertCount(4, $articleStructs);
 
-		$this->assertEquals('title1', $articleStruct['title']);
-		$this->assertEquals('text1', $articleStruct['text']);
-		$this->assertEquals('sport', $articleStruct['categoryName']);
+		$this->assertEquals('title1', $articleStructs['title']);
+		$this->assertEquals('text1', $articleStructs['text']);
+		$this->assertEquals('sport', $articleStructs['categoryName']);
 	}
 
 	function testPostDoArticleExpectExceptionBecauseCategoryNotFound() {
@@ -148,11 +148,11 @@ class ArticleControllerTest extends TestCase {
             		'text' => 'text1'
        			])
 				->exec();
-		$articleStruct = $response->parseJson();
-		$this->assertCount(4, $articleStruct);
-		$this->assertEquals('title1', $articleStruct['title']);
-		$this->assertEquals('text1', $articleStruct['text']);
-		$this->assertEquals('sport', $articleStruct['categoryName']);
+		$articleStructs = $response->parseJson();
+		$this->assertCount(4, $articleStructs);
+		$this->assertEquals('title1', $articleStructs['title']);
+		$this->assertEquals('text1', $articleStructs['text']);
+		$this->assertEquals('sport', $articleStructs['categoryName']);
 	}
 
 	/**
@@ -165,11 +165,11 @@ class ArticleControllerTest extends TestCase {
             		'title' => 'title1',
        			])
 				->exec();
-		$articleStruct = $response->parseJson();
-		$this->assertCount(4, $articleStruct);
-		$this->assertEquals('title1', $articleStruct['title']);
-		$this->assertEquals('Loren ipsum 1', $articleStruct['text']);
-		$this->assertEquals('news', $articleStruct['categoryName']);
+		$articleStructs = $response->parseJson();
+		$this->assertCount(4, $articleStructs);
+		$this->assertEquals('title1', $articleStructs['title']);
+		$this->assertEquals('Loren ipsum 1', $articleStructs['text']);
+		$this->assertEquals('news', $articleStructs['categoryName']);
 
 	}
 
@@ -183,11 +183,11 @@ class ArticleControllerTest extends TestCase {
             		'categoryName' => 'sport',
        			])
 				->exec();
-		$articleStruct = $response->parseJson();
-		$this->assertCount(4, $articleStruct);
-		$this->assertEquals('Title 1', $articleStruct['title']);
-		$this->assertEquals('Loren ipsum 1', $articleStruct['text']);
-		$this->assertEquals('sport', $articleStruct['categoryName']);
+		$articleStructs = $response->parseJson();
+		$this->assertCount(4, $articleStructs);
+		$this->assertEquals('Title 1', $articleStructs['title']);
+		$this->assertEquals('Loren ipsum 1', $articleStructs['text']);
+		$this->assertEquals('sport', $articleStructs['categoryName']);
 
 	}
 
@@ -201,11 +201,11 @@ class ArticleControllerTest extends TestCase {
             		'text' => 'text1',
        			])
 				->exec();
-		$articleStruct = $response->parseJson();
-		$this->assertCount(4, $articleStruct);
-		$this->assertEquals('Title 1', $articleStruct['title']);
-		$this->assertEquals('text1', $articleStruct['text']);
-		$this->assertEquals('news', $articleStruct['categoryName']);
+		$articleStructs = $response->parseJson();
+		$this->assertCount(4, $articleStructs);
+		$this->assertEquals('Title 1', $articleStructs['title']);
+		$this->assertEquals('text1', $articleStructs['text']);
+		$this->assertEquals('news', $articleStructs['categoryName']);
 
 	}
 
@@ -241,11 +241,11 @@ class ArticleControllerTest extends TestCase {
 
 	function testDeleteDoArticle() {
 		$response = TestEnv::http()->newRequest()
-			->delete(['api', 'article',$this->article1Id])
-			->exec();
-			$tx = TestEnv::createTransaction();
-			$this->assertEquals(2,TestEnv::temUtil()->count(Article::class));
-			$tx->commit();
+				->delete(['api', 'article',$this->article1Id])
+				->exec();
+		$tx = TestEnv::createTransaction();
+		$this->assertEquals(2,TestEnv::temUtil()->count(Article::class));
+		$tx->commit();
 	}