22
33namespace phpMyFAQ ;
44
5+ use phpMyFAQ \Attachment \AttachmentException ;
6+ use phpMyFAQ \Attachment \Filesystem \File \FileException ;
57use phpMyFAQ \Core \Exception ;
68use phpMyFAQ \Database \Sqlite3 ;
9+ use phpMyFAQ \Entity \FaqEntity ;
710use PHPUnit \Framework \TestCase ;
811
912class FaqTest extends TestCase
@@ -38,6 +41,18 @@ protected function setUp(): void
3841 $ this ->faq = new Faq ($ this ->configuration );
3942 }
4043
44+ /**
45+ * @throws AttachmentException
46+ * @throws FileException
47+ */
48+ protected function tearDown (): void
49+ {
50+ parent ::tearDown ();
51+
52+ $ faqEntity = $ this ->getFaqEntity ();
53+ $ this ->faq ->deleteRecord (1 , $ faqEntity ->getLanguage ());
54+ }
55+
4156 public function testSetGroups (): void
4257 {
4358 $ this ->assertInstanceOf (Faq::class, $ this ->faq ->setGroups ([-1 ]));
@@ -53,4 +68,109 @@ public function testHasTitleAHash(): void
5368 $ this ->assertTrue ($ this ->faq ->hasTitleAHash ('H#llo World! ' ));
5469 $ this ->assertFalse ($ this ->faq ->hasTitleAHash ('Hallo World! ' ));
5570 }
71+
72+ public function testCreate (): void
73+ {
74+ $ faqEntity = $ this ->getFaqEntity ();
75+
76+ // Call the method being tested
77+ $ result = $ this ->faq ->create ($ faqEntity );
78+
79+ // Assert that the method returns an integer
80+ $ this ->assertIsInt ($ result );
81+ $ this ->assertGreaterThan (0 , $ result );
82+ }
83+
84+ public function testGetNextSolutionId (): void
85+ {
86+ $ this ->assertIsInt ($ this ->faq ->getNextSolutionId ());
87+ $ this ->assertGreaterThan (0 , $ this ->faq ->getNextSolutionId ());
88+
89+ $ this ->faq ->create ($ this ->getFaqEntity ());
90+
91+ $ this ->assertGreaterThan (1 , $ this ->faq ->getNextSolutionId ());
92+ }
93+
94+ public function testUpdate (): void
95+ {
96+ $ faqEntity = $ this ->getFaqEntity ();
97+ $ faqEntity ->setId ($ this ->faq ->create ($ faqEntity ));
98+
99+ $ faqEntity ->setRevisionId (0 );
100+ $ faqEntity ->setQuestion ('Updated question ' );
101+ $ faqEntity ->setAnswer ('Updated answer ' );
102+
103+ $ result = $ this ->faq ->update ($ faqEntity );
104+
105+ $ this ->assertTrue ($ result );
106+ }
107+
108+ /**
109+ * @throws AttachmentException
110+ * @throws FileException
111+ */
112+ public function testDeleteRecord (): void
113+ {
114+ $ faqEntity = $ this ->getFaqEntity ();
115+ $ faqEntity ->setId ($ this ->faq ->create ($ faqEntity ));
116+
117+ $ result = $ this ->faq ->deleteRecord ($ faqEntity ->getId (), $ faqEntity ->getLanguage ());
118+
119+ $ this ->assertTrue ($ result );
120+ }
121+
122+ public function testGetSolutionIdFromId (): void
123+ {
124+ $ faqEntity = $ this ->getFaqEntity ();
125+ $ faqEntity ->setId ($ this ->faq ->create ($ faqEntity ));
126+
127+ $ this ->assertIsInt ($ this ->faq ->getSolutionIdFromId ($ faqEntity ->getId (), $ faqEntity ->getLanguage ()));
128+ $ this ->assertGreaterThan (0 , $ this ->faq ->getSolutionIdFromId ($ faqEntity ->getId (), $ faqEntity ->getLanguage ()));
129+ }
130+
131+ public function testHasTranslation (): void
132+ {
133+ $ faqEntity = $ this ->getFaqEntity ();
134+ $ faqEntity ->setId ($ this ->faq ->create ($ faqEntity ));
135+
136+ $ this ->assertTrue ($ this ->faq ->hasTranslation ($ faqEntity ->getId (), $ faqEntity ->getLanguage ()));
137+ $ this ->assertFalse ($ this ->faq ->hasTranslation ($ faqEntity ->getId (), 'de ' ));
138+ }
139+
140+ public function testIsActive (): void
141+ {
142+ $ faqEntity = $ this ->getFaqEntity ();
143+ $ faqEntity ->setId ($ this ->faq ->create ($ faqEntity ));
144+
145+ $ this ->assertTrue ($ this ->faq ->isActive ($ faqEntity ->getId (), $ faqEntity ->getLanguage ()));
146+ }
147+
148+ public function testGetRecordBySolutionId (): void
149+ {
150+ $ faqEntity = $ this ->getFaqEntity ();
151+ $ faqEntity ->setSolutionId (42 );
152+ $ this ->faq ->create ($ faqEntity );
153+
154+ $ this ->faq ->getRecordBySolutionId (42 );
155+
156+ $ this ->assertEquals (1 , $ faqEntity ->getId ());
157+ }
158+
159+ private function getFaqEntity (): FaqEntity
160+ {
161+ $ faqEntity = new FaqEntity ();
162+ $ faqEntity
163+ ->setLanguage ('en ' )
164+ ->setActive (true )
165+ ->setSticky (true )
166+ ->setKeywords ('Keywords ' )
167+ ->setQuestion ('Question ' )
168+ ->setAnswer ('Answer ' )
169+ ->setAuthor ('Author ' )
170+ 171+ ->setComment (true )
172+ ->setNotes ('' );
173+
174+ return $ faqEntity ;
175+ }
56176}
0 commit comments