File indexing completed on 2024-12-22 03:47:06

0001 <?php
0002 /*
0003     SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org>
0004 
0005     SPDX-License-Identifier: MIT
0006 */
0007 
0008 require_once('datastoretesthelper.php');
0009 require_once('../src/server/shared/product.php');
0010 require_once('../src/server/shared/survey.php');
0011 
0012 class SurveyTest extends PHPUnit\Framework\TestCase
0013 {
0014     private static $db;
0015 
0016     public static function setUpBeforeClass(): void
0017     {
0018         self::$db = DatastoreTestHelper::setup();
0019     }
0020 
0021     public function testFromJson()
0022     {
0023         $s = Survey::fromJson('{
0024             "name": "mySurvey",
0025             "url": "http://survey.example/",
0026             "active": true,
0027             "target": "screen[0].dpi > 100",
0028             "uuid": "{9e529dfa-0213-413e-a1a8-8a9cea7d5a97}"
0029         }');
0030         $this->assertEquals($s->name, 'mySurvey');
0031         $this->assertEquals($s->url, 'http://survey.example/');
0032         $this->assertEquals($s->active, true);
0033         $this->assertEquals($s->uuid, '{9e529dfa-0213-413e-a1a8-8a9cea7d5a97}');
0034         $this->assertEquals($s->target, 'screen[0].dpi > 100');
0035     }
0036 
0037     public function testToJson()
0038     {
0039         $s = new Survey();
0040         $s->name = 'mySurvey';
0041         $s->url = 'http://survey.example/';
0042         $s->active = true;
0043         $s->uuid = '{9e529dfa-0213-413e-a1a8-8a9cea7d5a97}';
0044         $s->target = 'screen[0].dpi > 100';
0045 
0046         $this->assertJsonStringEqualsJsonString(json_encode($s), '{
0047             "uuid": "{9e529dfa-0213-413e-a1a8-8a9cea7d5a97}",
0048             "name": "mySurvey",
0049             "url": "http://survey.example/",
0050             "active": true,
0051             "target": "screen[0].dpi > 100"
0052         }');
0053     }
0054 
0055     public function testGetSurveys()
0056     {
0057         $surveys = Survey::surveysForProduct(self::$db, 'I don\'t exist');
0058         $this->assertCount(0, $surveys);
0059 
0060         $surveys = Survey::surveysForProduct(self::$db, 'org.kde.UnitTest');
0061         $this->assertCount(2, $surveys);
0062 
0063         $survey1 = $surveys[0];
0064         $this->assertInstanceOf(Survey::class, $survey1);
0065         $survey2 = $surveys[1];
0066         $this->assertInstanceOf(Survey::class, $survey2);
0067 
0068         if ($survey1->name > $survey2->name) {
0069             $survey1 = $surveys->schema[1];
0070             $survey2 = $surveys->schema[0];
0071         }
0072 
0073         $this->assertEquals($survey1->name, 'myActiveSurvey');
0074         $this->assertEquals($survey1->url, 'http://survey.example/active');
0075         $this->assertEquals($survey1->active, true);
0076         $this->assertEquals($survey1->target, 'usageTime.value >= 3600');
0077 
0078         $this->assertEquals($survey2->name, 'myInactiveSurvey');
0079         $this->assertEquals($survey2->url, 'http://survey.example/inactive');
0080         $this->assertEquals($survey2->active, false);
0081         $this->assertEquals($survey2->target, 'screen[0].dpi < 200');
0082 
0083         $p = Product::productByName(self::$db, 'org.kde.UnitTest');
0084         $this->assertNotNull($p);
0085         $surveys = Survey::activeSurveysForProduct(self::$db, $p);
0086         $this->assertCount(1, $surveys);
0087 
0088         $s = $surveys[0];
0089         $this->assertInstanceOf(Survey::class, $s);
0090         $this->assertEquals($s->name, 'myActiveSurvey');
0091         $this->assertEquals($s->url, 'http://survey.example/active');
0092         $this->assertEquals($s->active, true);
0093         $this->assertEquals($s->target, 'usageTime.value >= 3600');
0094     }
0095 
0096     public function testSurveyInsert()
0097     {
0098         $json = '{
0099             "name": "newSurvey",
0100             "uuid": "{9e529dfa-0213-413e-a1a8-8a9cea7d5a97}",
0101             "url": "http://survey.example/new",
0102             "active": true,
0103             "target": "startCount.value > 10"
0104         }';
0105 
0106         $p = Product::productByName(self::$db, 'org.kde.UnitTest');
0107         $this->assertNotNull($p);
0108 
0109         $s = Survey::fromJson($json);
0110         $s->insert(self::$db, $p);
0111 
0112         $surveys = Survey::activeSurveysForProduct(self::$db, $p);
0113         $s = null;
0114         foreach ($surveys as $survey) {
0115             if ($survey->name == 'newSurvey') {
0116                 $s = $survey;
0117                 break;
0118             }
0119         }
0120         $this->assertNotNull($s);
0121         $this->assertEquals($s->name, 'newSurvey');
0122         $this->assertEquals($s->url, 'http://survey.example/new');
0123         $this->assertEquals($s->active, true);
0124         $this->assertEquals($s->target, 'startCount.value > 10');
0125         $this->assertEquals($s->uuid, '{9e529dfa-0213-413e-a1a8-8a9cea7d5a97}');
0126     }
0127 
0128     public function testSurveyUpdate()
0129     {
0130         $json = '{
0131             "name": "myChangedSurvey",
0132             "url": "http://survey.example/changed",
0133             "active": false,
0134             "uuid": "{962bbd80-7120-4f18-a4c0-5800fa323868}",
0135             "target": "views[\"editor\"].ratio > 0.5"
0136         }';
0137 
0138         $s = Survey::fromJson($json);
0139         $s->update(self::$db);
0140 
0141         $surveys = Survey::surveysForProduct(self::$db, 'org.kde.UnitTest');
0142         $s = null;
0143         foreach ($surveys as $survey) {
0144             if ($survey->name == 'myChangedSurvey') {
0145                 $s = $survey;
0146                 break;
0147             }
0148         }
0149         $this->assertNotNull($s);
0150         $this->assertEquals($s->name, 'myChangedSurvey');
0151         $this->assertEquals($s->url, 'http://survey.example/changed');
0152         $this->assertEquals($s->active, false);
0153         $this->assertEquals($s->target, 'views["editor"].ratio > 0.5');
0154     }
0155 
0156     public function testSurveyDelete()
0157     {
0158         $json = '{
0159             "name": "myInactiveSurvey",
0160             "url": "http://survey.example/inactive",
0161             "active": false,
0162             "uuid": "{bdfa82c5-238f-404b-a441-07ca3d3eff7f}"
0163         }';
0164 
0165         $s = Survey::fromJson($json);
0166         $s->delete(self::$db);
0167 
0168         $surveys = Survey::surveysForProduct(self::$db, 'org.kde.UnitTest');
0169         $s = null;
0170         foreach ($surveys as $survey) {
0171             if ($survey->name == 'myInactiveSurvey') {
0172                 $s = $survey;
0173                 break;
0174             }
0175         }
0176         $this->assertNull($s);
0177     }
0178 
0179     public static function invalidInput_data()
0180     {
0181         return [
0182             'empty' => [ '{}' ],
0183             'mising url' => [ '{ "name": "surveyName" }' ],
0184             'missing active' => [ '{ "name": "surveyName", "url": "http://survey.example/" }' ],
0185             'empty name' => [ '{ "name": "", "url": "http://survey.example/", "active": false }' ],
0186             'non-string name' => [ '{ "name": 123, "url": "http://survey.example/", "active": false }' ],
0187             'non-numberic id' => [ '{ "name": "myName", "url": "http://survey.example/", "active": true, "id": "string" }' ]
0188         ];
0189     }
0190 
0191     /**
0192      * @dataProvider invalidInput_data
0193      */
0194     public function testInvalidInput($json)
0195     {
0196         $this->expectException(RESTException::class);
0197         $this->expectExceptionCode(400);
0198         $s = Survey::fromJson($json);
0199     }
0200 }
0201 
0202