Commit 5f50a946 authored by Fabrice Gangler's avatar Fabrice Gangler 🎨
Browse files

TEST(software): add tests for add and edit forms

add checks for software forms (ADD and EDIT)
with or without "userOf" or "ProviderFor"
parameters (or equal to "no")

Refs: #880 #881
parent 614d5821
......@@ -1374,4 +1374,520 @@ class SoftwaresControllerTest extends ApiIntegrationTestCase
WWW_ROOT . 'img' . DS . "files/Screenshots/" . time() . "_" . $this->wrongFile["name"]
);
}
////// Checks for the ADD software form with or without "userOf" or "ProviderFor" parameters (or equal to "no")
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Common checks for the Add Software Form
* - Add new software by sending an HTTP Post request (the UI of the form is not tested)
* - Check that the software now exists in the database.
* - Check that the main data is correctly recorded in the database.
* - Check if current HTTP response is a redirection to the software URL.
*
* @param array $extraData additional data for the form (addition or overloading)
* @param string $redirectType 'softwares' or 'mappingForm'
* @return int ID of added software
*/
private function commonCheckForHtmlFormAddSoftware(array $extraData = [], string $redirectType = 'softwares')
{
$softwareName = 'My new software '. mt_rand();
$commonData = [
'softwarename' => $softwareName,
'url_repository' => 'http://www'. mt_rand() .'.mynewsoftware.com/',
'description' => 'This is my new software and...',
'licence_id' => 1,
"url_website" => null,
"tag_string" => "test aze great",
];
// Cheking that the software does not exist in the database before creating it.
$this->assertEquals(0, $this->Softwares->find()->where(['softwarename' => $softwareName])->count());
// Add software
$dataForm = $commonData;
foreach ($extraData as $key => $value) {
$dataForm[$key] = $value;
}
$this->post('/fr/softwares/add', $dataForm);
$this->assertEventFired('Model.Software.created', $this->Softwares->eventManager());
// Cheking that the software now exists in the database and grab it's ID.
$this->assertEquals(1, $this->Softwares->find()->where(['softwarename' => $softwareName])->count());
$software = $this->Softwares->find()->where(['softwarename' => $softwareName])->limit(1)->toList()[0];
$softwareId = $software->id;
// Cheking if current HTTP response is a redirection to the software URL
$redirectTo = "/fr/$redirectType/$softwareId";
$this->currentResponseIsRedirectionToAnotherUrl($redirectTo);
// Check that the main data is correctly recorded in the database.
$dataInDb = [
'softwarename' => $software->get('softwarename'),
'url_repository' => $software->get('url_repository'),
'description' => $software->get('description'),
'licence_id' => $software->get('licence_id'),
"url_website" => $software->get('url_website'),
"tag_string" => $software->get('tag_string'),
];
$this->assertTrue($this->arraysAreSimilar($commonData, $dataInDb));
// return ID of added software
return $softwareId;
}
/**
* Common checks for the Add Software Form
* without "userOf" or "ProviderFor" parameters (or equal to "no")
* - (1) Add new software by sending an HTTP Post request (the UI of the form is not tested)
* - (1) Check that the software now exists in the database.
* - (1) Check that the main data is correctly recorded in the database.
* - (1) Check if current HTTP response is a redirection to the software URL.
* - For current connected user and this new software:
* - Check if there are 0 records in 'TaxonomysSoftwares' table
* - Check if there are 0 records in 'RelationshipsSoftwaresUsers' table
*
* (1) via commonCheckForHtmlFormAddSoftware()
*
* @param array $extraData additional data for the form (addition or overloading)
*/
private function commonCheckForHtmlFormAddSoftwareWithoutExtraFormData($extraData = [])
{
$softwareId = $this->commonCheckForHtmlFormAddSoftware($extraData);
$userId = $this->getConnectedUserId();
// Check if there are 0 records in 'TaxonomysSoftwares' table
// for this user and this software
$registry = TableRegistry::get('TaxonomysSoftwares');
$nb = $registry->find()->where(["software_id" => $softwareId, "user_id" => $userId])->count();
$this->assertEquals(0, $nb);
// Check if there are 0 records in 'RelationshipsSoftwaresUsers' table
// for this user and this software
$registry = TableRegistry::get('RelationshipsSoftwaresUsers');
$nb = $registry->find()->where(["software_id" => $softwareId, "user_id" => $userId])->count();
$this->assertEquals(0, $nb);
}
/**
* Common checks for the Add Software Form
* with "userOf" or "ProviderFor" parameters equal to "yes".
* - (1) Add new software by sending an HTTP Post request (the UI of the form is not tested)
* - (1) Check that the software now exists in the database.
* - (1) Check that the main data is correctly recorded in the database.
* - (1) Check if current HTTP response is a redirection to the software URL.
* - For this user and this new software:
* - Check if there are 0 records in 'TaxonomysSoftwares' table
* - Check if there are 1 records in 'RelationshipsSoftwaresUsers' table
* - Check if there are 1 records in 'RelationshipsSoftwaresUsers' table, with correct relationship ID
*
* (1) via commonCheckForHtmlFormAddSoftware()
*
* @param string $redirectType 'softwares' or 'mappingForm'
* @param array $extraData additional data for the form (addition or overloading)
*/
private function commonCheckForHtmlFormAddSoftwareWithExtraFormData(
string $relationshipName,
string $redirectType = 'softwares'
) {
// Launch common tests
$extraData = [$relationshipName => 'yes'];
$softwareId = $this->commonCheckForHtmlFormAddSoftware($extraData, $redirectType);
// Get connected user ID
$userId = $this->getConnectedUserId();
$conditions = [
"software_id" => $softwareId,
"user_id" => $userId
];
// Check if there are 0 records in 'TaxonomysSoftwares' table
// for this user and this software
$registry = TableRegistry::get('TaxonomysSoftwares');
$nb = $registry->find()->where($conditions)->count();
$this->assertEquals(0, $nb);
// Check if there are 1 records in 'RelationshipsSoftwaresUsers' table
// for this user and this software
$registry = TableRegistry::get('RelationshipsSoftwaresUsers');
$nb = $registry->find()->where($conditions)->count();
$this->assertEquals(1, $nb);
// Get relationship ID for current relationship
$registry = TableRegistry::get('Relationships');
$relationship = $registry->find("all")->where(["cd" => ucfirst($relationshipName)])->first();
$conditions['relationship_id'] = $relationship->id;
// Check if there are 1 records in 'RelationshipsSoftwaresUsers' table
// for this user, this software and current relationship
$registry = TableRegistry::get('RelationshipsSoftwaresUsers');
$nb = $registry->find()->where($conditions)->count();
$this->assertEquals(1, $nb);
}
/**
* Test HTML form:
* for the different user types ("Person", "Association", "Administration" and "Company"),
* add new software by sending an HTTP Post request (the UI of the form is not tested)
* without "userOf" or "ProviderFor" parameters (or equal to "no").
*
* - (1) Add new software by sending an HTTP Post request (the UI of the form is not tested)
* - (1) Check that the software now exists in the database.
* - (1) Check that the main data is correctly recorded in the database.
* - (1) Check if current HTTP response is a redirection to the software URL.
* - For this user and this new software:
* - (2) Check if there are 0 records in 'TaxonomysSoftwares' table
* - (2) Check if there are 0 records in 'RelationshipsSoftwaresUsers' table
*
* (1) via commonCheckForHtmlFormAddSoftware()
* (2) via commonCheckForHtmlFormAddSoftwareWithoutExtraFormData()
*
* @group user
* @group user_person
* @group software
* @group software_add
* @group software_methode_add
*/
public function testHtmlFormAddSoftwareWithoutExtraFormData()
{
// "Person" user
$this->setConnectedPersonSession();
$this->commonCheckForHtmlFormAddSoftwareWithoutExtraFormData([]); // without "userOf" or "ProviderFor" parameter
$this->commonCheckForHtmlFormAddSoftwareWithoutExtraFormData(['userOf' => 'no']);
$this->commonCheckForHtmlFormAddSoftwareWithoutExtraFormData(['userOf' => 'hackerInForm']);
// $this->commonCheckForHtmlFormAddSoftwareWithoutExtraFormData(['providerFor' => 'yes']); // ----> must FAIL
// "Association" user
$this->setConnectedAssociationSession();
$this->commonCheckForHtmlFormAddSoftwareWithoutExtraFormData([]); // without "userOf" or "ProviderFor" parameter
$this->commonCheckForHtmlFormAddSoftwareWithoutExtraFormData(['userOf' => 'no']);
$this->commonCheckForHtmlFormAddSoftwareWithoutExtraFormData(['userOf' => 'hackerInForm']);
// "Administration" user
$this->setConnectedAdministrationSession();
$this->commonCheckForHtmlFormAddSoftwareWithoutExtraFormData([]); // without "userOf" or "ProviderFor" parameter
$this->commonCheckForHtmlFormAddSoftwareWithoutExtraFormData(['userOf' => 'no']);
$this->commonCheckForHtmlFormAddSoftwareWithoutExtraFormData(['userOf' => 'hackerInForm']);
// "Administration" user, with "admin" role @@@TODO
$this->setAdminSessionOfAdministrationType();
$this->commonCheckForHtmlFormAddSoftwareWithoutExtraFormData(['userOf' => 'no']);
// "Company" user
$this->setConnectedCompanySession();
$this->commonCheckForHtmlFormAddSoftwareWithoutExtraFormData([]); // without "userOf" or "ProviderFor" parameter
$this->commonCheckForHtmlFormAddSoftwareWithoutExtraFormData(['providerFor' => 'no']);
$this->commonCheckForHtmlFormAddSoftwareWithoutExtraFormData(['providerFor' => 'hackerInForm']);
}
/**
* Test HTML form:
* for the different user types ("Person", "Association", "Administration" and "Company"),
* add new software by sending an HTTP Post request (the UI of the form is not tested)
* with "userOf" or "ProviderFor" parameters equal to "yes".
*
* - (1) Add new software by sending an HTTP Post request (the UI of the form is not tested)
* - (1) Check that the software now exists in the database.
* - (1) Check that the main data is correctly recorded in the database.
* - (1) Check if current HTTP response is a redirection to the software URL.
* - For this user and this new software:
* - (2) Check if there are 0 records in 'TaxonomysSoftwares' table
* - (2) Check if there are 1 records in 'RelationshipsSoftwaresUsers' table
* - (2) Check if there are 1 records in 'RelationshipsSoftwaresUsers' table, with correct relationship ID
*
* (1) via commonCheckForHtmlFormAddSoftware()
* (2) via commonCheckForHtmlFormAddSoftwareWithExtraFormData()
*
* @group user
* @group user_person
* @group software
* @group software_add
* @group software_methode_add
*/
public function testHtmlFormAddSoftwareWithExtraFormData()
{
// "Person" user
$this->setConnectedPersonSession();
$this->commonCheckForHtmlFormAddSoftwareWithExtraFormData('userOf');
// "Association" user
$this->setConnectedAssociationSession();
$this->commonCheckForHtmlFormAddSoftwareWithExtraFormData('userOf');
// "Administration" user
$this->setConnectedAdministrationSession();
$this->commonCheckForHtmlFormAddSoftwareWithExtraFormData('userOf');
// "Administration" user, with "admin" role @@@TODO
$this->setAdminSessionOfAdministrationType();
$this->commonCheckForHtmlFormAddSoftwareWithExtraFormData('userOf', 'mappingForm');
// "Company" user
$this->setConnectedCompanySession();
$this->commonCheckForHtmlFormAddSoftwareWithExtraFormData('providerFor');
}
////// Checks for the EDIT software form with or without "userOf" or "ProviderFor" parameters (or equal to "no")
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Common checks for the Edit Software Form
* - Edit a software by sending an HTTP Post request (the UI of the form is not tested)
* - Check that the main data is correctly recorded in the database.
* - Check if current HTTP response is a redirection to the software URL.
*
* @param array $extraData additional data for the form (addition or overloading)
* @param string $redirectType 'softwares' or 'mappingForm'
* @return int ID of edited software
*/
private function commonCheckForHtmlFormEditSoftware(
int $softwareId,
array $extraData = [],
string $redirectType = 'softwares'
) {
// Grab a software in the database before updating it.
$software = $this->Softwares->find()->where(['id' => $softwareId])->toList()[0];
$alea = mt_rand();
$newSoftwareName = $software->get('softwarename'). " $alea";
$commonData = [
'softwarename' => $newSoftwareName,
'url_repository' => $software->get('url_repository') . "/$alea",
'description' => $software->get('description') . " $alea",
'licence_id' => $software->get('licence_id'),
"url_website" => $software->get('url_website') . "/$alea",
"tag_string" => $software->get('tag_string'),
];
// Edit software
$dataForm = $commonData;
foreach ($extraData as $key => $value) {
$dataForm[$key] = $value;
}
$this->put("/fr/softwares/edit/$softwareId", $dataForm);
$this->assertEventFired('Model.Software.modified', $this->Softwares->eventManager());
// Cheking that the software now exists with its new name
// in the database and grab it's ID.
$this->assertEquals(
1,
$this->Softwares
->find()
->where(['softwarename' => $newSoftwareName ])
->count()
);
$softwareUpdated = $this->Softwares->find()->where(['id' => $softwareId ])->limit(1)->toList()[0];
$this->assertEquals($softwareId, $softwareUpdated->id);
// Cheking if current HTTP response is a redirection to the software URL
$redirectTo = "/fr/$redirectType/$softwareId";
$this->currentResponseIsRedirectionToAnotherUrl($redirectTo);
// Check that the main data is correctly recorded in the database.
$dataInDb = [
'softwarename' => $softwareUpdated->get('softwarename'),
'url_repository' => $softwareUpdated->get('url_repository'),
'description' => $softwareUpdated->get('description'),
'licence_id' => $softwareUpdated->get('licence_id'),
"url_website" => $softwareUpdated->get('url_website'),
"tag_string" => $softwareUpdated->get('tag_string'),
];
$this->assertTrue($this->arraysAreSimilar($commonData, $dataInDb));
// return ID of added software
return $softwareId;
}
/**
* Common checks for the Edit Software Form
* without "userOf" or "ProviderFor" parameters (or equal to "no")
* - (1) Edit a software by sending an HTTP Post request (the UI of the form is not tested)
* - (1) Check that the main data is correctly recorded in the database.
* - (1) Check if current HTTP response is a redirection to the software URL.
* - For current connected user and this new software:
* - Check if there are 0 records in 'TaxonomysSoftwares' table
* - Check if there are 0 records in 'RelationshipsSoftwaresUsers' table
*
* (1) via commonCheckForHtmlFormEditSoftware()
*
* @param array $extraData additional data for the form (addition or overloading)
*/
private function commonCheckForHtmlFormEditSoftwareWithoutExtraFormData($extraData = [])
{
$softwareId = 2;
$softwareId = $this->commonCheckForHtmlFormEditSoftware($softwareId, $extraData);
$userId = $this->getConnectedUserId();
// Check if there are 0 records in 'RelationshipsSoftwaresUsers' table
// for this user and this software
$registry = TableRegistry::get('RelationshipsSoftwaresUsers');
$nb = $registry->find()->where(["software_id" => $softwareId, "user_id" => $userId])->count();
$this->assertEquals(0, $nb);
}
/**
* Common checks for the Edit Software Form
* with "userOf" or "ProviderFor" parameters equal to "yes".
* - (1) Add new software by sending an HTTP Post request (the UI of the form is not tested)
* - (1) Check that the main data is correctly recorded in the database.
* - (1) Check if current HTTP response is a redirection to the software URL.
* - For this user and this new software:
* - Check if there are 1 records in 'RelationshipsSoftwaresUsers' table
* - Check if there are 1 records in 'RelationshipsSoftwaresUsers' table, with correct relationship ID
*
* (1) via commonCheckForHtmlFormEditSoftware()
*
* @param string $redirectType 'softwares' or 'mappingForm'
* @param array $extraData additional data for the form (addition or overloading)
*/
private function commonCheckForHtmlFormEditSoftwareWithExtraFormData(
string $relationshipName,
string $redirectType = 'softwares'
) {
$softwareId = 2;
// Get connected user ID
$userId = $this->getConnectedUserId();
$conditions = [
"software_id" => $softwareId,
"user_id" => $userId
];
// Before editing software, for this user and this software,
// check if there are 0 records in 'RelationshipsSoftwaresUsers' table
$registry = TableRegistry::get('RelationshipsSoftwaresUsers');
$nb = $registry->find()->where($conditions)->count();
$this->assertEquals(0, $nb);
// Launch common tests
$extraData = [$relationshipName => 'yes'];
$this->commonCheckForHtmlFormEditSoftware($softwareId, $extraData, $redirectType);
// Check if there are 1 records in 'RelationshipsSoftwaresUsers' table
// for this user and this software
$registry = TableRegistry::get('RelationshipsSoftwaresUsers');
$nb = $registry->find()->where($conditions)->count();
$this->assertEquals(1, $nb);
// Get relationship ID for current relationship
$registry = TableRegistry::get('Relationships');
$relationship = $registry->find("all")->where(["cd" => ucfirst($relationshipName)])->first();
$conditions['relationship_id'] = $relationship->id;
// Check if there are 1 records in 'RelationshipsSoftwaresUsers' table
// for this user, this software and current relationship
$registry = TableRegistry::get('RelationshipsSoftwaresUsers');
$nb = $registry->find()->where($conditions)->count();
$this->assertEquals(1, $nb);
}
/**
* Test HTML form:
* for the different user types ("Person", "Association", "Administration" and "Company"),
* edit a software by sending an HTTP Post request (the UI of the form is not tested)
* without "userOf" or "ProviderFor" parameters (or equal to "no").
*
* - (1) Edit new software by sending an HTTP Post request (the UI of the form is not tested)
* - (1) Check that the main data is correctly recorded in the database.
* - (1) Check if current HTTP response is a redirection to the software URL.
* - For this user and this software:
* - (2) Check if there are 0 records in 'RelationshipsSoftwaresUsers' table
*
* (1) via commonCheckForHtmlFormEditSoftware()
* (2) via commonCheckForHtmlFormEditSoftwareWithoutExtraFormData()
*
* @group user
* @group user_person
* @group software
* @group software_edit
* @group software_methode_edit
*/
public function testHtmlFormEditSoftwareWithoutExtraFormData()
{
// "Person" user
$this->setConnectedPersonSession();
$this->commonCheckForHtmlFormEditSoftwareWithoutExtraFormData([]); // without userOf or ProviderFor parameter
$this->commonCheckForHtmlFormEditSoftwareWithoutExtraFormData(['userOf' => 'no']);
$this->commonCheckForHtmlFormEditSoftwareWithoutExtraFormData(['userOf' => 'hackerInForm']);
// $this->commonCheckForHtmlFormEditSoftwareWithoutExtraFormData(['providerFor' => 'yes']); // ----> must FAIL
// "Association" user
$this->setConnectedAssociationSession();
$this->commonCheckForHtmlFormEditSoftwareWithoutExtraFormData([]); // without userOf or ProviderFor parameter
$this->commonCheckForHtmlFormEditSoftwareWithoutExtraFormData(['userOf' => 'no']);
$this->commonCheckForHtmlFormEditSoftwareWithoutExtraFormData(['userOf' => 'hackerInForm']);
// "Administration" user
$this->setConnectedAdministrationSession();
$this->commonCheckForHtmlFormEditSoftwareWithoutExtraFormData([]); // without userOf or ProviderFor parameter
$this->commonCheckForHtmlFormEditSoftwareWithoutExtraFormData(['userOf' => 'no']);
$this->commonCheckForHtmlFormEditSoftwareWithoutExtraFormData(['userOf' => 'hackerInForm']);
// "Administration" user, with "admin" role @@@TODO
$this->setAdminSessionOfAdministrationType();
$this->commonCheckForHtmlFormEditSoftwareWithoutExtraFormData(['userOf' => 'no']);
// "Company" user
$this->setConnectedCompanySession();
$this->commonCheckForHtmlFormEditSoftwareWithoutExtraFormData([]); // without userOf or ProviderFor parameter
$this->commonCheckForHtmlFormEditSoftwareWithoutExtraFormData(['providerFor' => 'no']);
$this->commonCheckForHtmlFormEditSoftwareWithoutExtraFormData(['providerFor' => 'hackerInForm']);
}
/**
* Test HTML form:
* for the different user types ("Person", "Association", "Administration" and "Company"),
* edit a software by sending an HTTP Post request (the UI of the form is not tested)
* with "userOf" or "ProviderFor" parameters equal to "yes".
*
* - (1) Edit a software by sending an HTTP Post request (the UI of the form is not tested)
* - (1) Check that the main data is correctly recorded in the database.
* - (1) Check if current HTTP response is a redirection to the software URL.
* - For this user and this new software:
* - (2) Check if there are 1 records in 'RelationshipsSoftwaresUsers' table
* - (2) Check if there are 1 records in 'RelationshipsSoftwaresUsers' table, with correct relationship ID
*
* (1) via commonCheckForHtmlFormEditSoftware()
* (2) via commonCheckForHtmlFormEditSoftwareWithExtraFormData()
*
* @group user
* @group user_person
* @group software
* @group software_edit
* @group software_methode_edit
*/
public function testHtmlFormEditSoftwareWithExtraFormData()
{
// "Person" user
$this->setConnectedPersonSession();
$this->commonCheckForHtmlFormEditSoftwareWithExtraFormData('userOf');
// "Association" user
$this->setConnectedAssociationSession();
$this->commonCheckForHtmlFormEditSoftwareWithExtraFormData('userOf');
// "Administration" user
$this->setConnectedAdministrationSession();
$this->commonCheckForHtmlFormEditSoftwareWithExtraFormData('userOf');
// "Administration" user, with "admin" role @@@TODO
$this->setAdminSessionOfAdministrationType();
$this->commonCheckForHtmlFormEditSoftwareWithExtraFormData('userOf', 'mappingForm');
// "Company" user
$this->setConnectedCompanySession();
$this->commonCheckForHtmlFormEditSoftwareWithExtraFormData('providerFor');
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment