/** * Get redmine issue matching this harvest entry and in the right project. * * @param \Harvest\Model\DayEntry $entry * * @return array|bool * Array of redmine issue information or FALSE if no match found */ protected function getRedmineIssue(DayEntry $entry) { // Load the Redmine issue and check if the Harvest time entry ID is there, if so, skip. $redmine_issue_numbers = []; preg_match('/#([0-9]+)/', $entry->get('notes'), $redmine_issue_numbers); // Strip the leading '#', and take the first entry. $redmine_issue_number = reset($redmine_issue_numbers); $redmine_issue_number = str_replace('#', '', $redmine_issue_number); if (!$redmine_issue_number) { // The resulting value is not a number. $this->userTimeEntryErrors[$entry->get('user-id')]['no-number'][] = ['entry' => $entry]; $this->output->writeln('<comment>Skipping entry, it does not look like there is an issue number here.'); return false; } $this->setRedmineClient(); $issue_api = new Redmine\Api\Issue($this->redmineClient); $redmine_issue = $issue_api->show($redmine_issue_number); if (!$redmine_issue || !isset($redmine_issue['issue']['project']['id'])) { // Issue doesn't exist in Redmine; this is probably a GitHub issue reference. $this->userTimeEntryErrors[$entry->get('user-id')]['missing-issue'][] = ['entry' => $entry]; $this->output->writeln(sprintf('<error>- Could not find Redmine issue %d!</error>', $redmine_issue_number)); $this->errors = true; return false; } // Validate that issue ID exists in project. if (isset($this->projectMap[$entry->get('project-id')])) { $project_names = []; $found = false; foreach ($this->projectMap[$entry->get('project-id')] as $project) { $project_names[] = current($project); if (isset($project[$redmine_issue['issue']['project']['id']])) { $found = true; } } if (!$found) { // The issue number doesn't belong to the Harvest project we are looking at // time entries for, so continue. It's probably a GitHub issue ref. $this->userTimeEntryErrors[$entry->get('user-id')]['issue-not-in-project'][] = ['entry' => $entry]; $this->output->writeln(sprintf('<error>- Issue %d does not exist in the Redmine project(s) %s. Time entry: \'%s\'</error>', $redmine_issue_number, implode(',', $project_names), $entry->toXML())); $this->errors = true; return false; } } return $redmine_issue; }
/** * Test buildXML() * * @covers ::buildXML * @test * * @return void */ public function testBuildXmlWithWatcherAndUploadAndCustomFieldAndStandard() { // Test values $parameters = array('watcher_user_ids' => array(5), 'subject' => 'Issue subject', 'uploads' => array(array('token' => 'first-token', 'filename' => 'SomeRandomFile.txt', 'description' => 'Simple description', 'content_type' => 'text/plain')), 'custom_fields' => array(array('id' => 25, 'value' => 'Second Custom Field'))); // Create the used mock objects $client = $this->getMockBuilder('Redmine\\Client')->disableOriginalConstructor()->getMock(); $client->expects($this->once())->method('post')->with('/issues.xml', $this->logicalAnd($this->stringStartsWith('<?xml version="1.0"?>' . "\n" . '<issue>'), $this->stringEndsWith('</issue>' . "\n"), $this->stringContains('<watcher_user_ids type="array">'), $this->stringContains('</watcher_user_ids>'), $this->stringContains('<watcher_user_id>5</watcher_user_id>'), $this->stringContains('<upload>' . '<token>first-token</token>' . '<filename>SomeRandomFile.txt</filename>' . '<description>Simple description</description>' . '<content_type>text/plain</content_type>' . '</upload>'), $this->stringContains('<custom_field id="25">' . '<value>Second Custom Field</value>' . '</custom_field>'), $this->stringContains('<subject>Issue subject</subject>'))); // Create the object under test $api = new Issue($client); // Perform the tests $api->create($parameters); }