diff options
| -rw-r--r-- | app/Functions/FunctionsImport.php | 14 | ||||
| -rw-r--r-- | app/GedcomRecord.php | 4 | ||||
| -rw-r--r-- | app/Tree.php | 20 | ||||
| -rw-r--r-- | tests/TestCase.php | 2 | ||||
| -rw-r--r-- | tests/app/TreeTest.php | 37 |
5 files changed, 56 insertions, 21 deletions
diff --git a/app/Functions/FunctionsImport.php b/app/Functions/FunctionsImport.php index 87484d62db..da52e38f32 100644 --- a/app/Functions/FunctionsImport.php +++ b/app/Functions/FunctionsImport.php @@ -17,7 +17,6 @@ declare(strict_types=1); namespace Fisharebest\Webtrees\Functions; -use Fisharebest\Webtrees\Database; use Fisharebest\Webtrees\Date; use Fisharebest\Webtrees\Gedcom; use Fisharebest\Webtrees\GedcomRecord; @@ -1015,12 +1014,12 @@ class FunctionsImport ]); } else { DB::table('name')->insert([ - 'n_file' => $ged_id, - 'n_id' => $xref, - 'n_num' => $n, - 'n_type' => $name['type'], - 'n_sort' => mb_substr($name['sort'], 0, 255), - 'n_full' => mb_substr($name['fullNN'], 0, 255), + 'n_file' => $ged_id, + 'n_id' => $xref, + 'n_num' => $n, + 'n_type' => $name['type'], + 'n_sort' => mb_substr($name['sort'], 0, 255), + 'n_full' => mb_substr($name['fullNN'], 0, 255), ]); } } @@ -1221,7 +1220,6 @@ class FunctionsImport ->where('pl_file', '=', $tree->id()) ->count(); - if ($num === 0) { DB::table('places') ->where('p_id', '=', $p_id) diff --git a/app/GedcomRecord.php b/app/GedcomRecord.php index c9e0937da2..fe8ab64816 100644 --- a/app/GedcomRecord.php +++ b/app/GedcomRecord.php @@ -67,10 +67,10 @@ class GedcomRecord protected $getSecondaryName; /** @var GedcomRecord[][] Allow getInstance() to return references to existing objects */ - protected static $gedcom_record_cache; + public static $gedcom_record_cache; /** @var stdClass[][] Fetch all pending edits in one database query */ - private static $pending_record_cache; + public static $pending_record_cache; /** * Create a GedcomRecord object from raw GEDCOM data. diff --git a/app/Tree.php b/app/Tree.php index d6026e9d47..be8ee601e6 100644 --- a/app/Tree.php +++ b/app/Tree.php @@ -23,9 +23,9 @@ use Fisharebest\Webtrees\Functions\FunctionsImport; use Illuminate\Database\Capsule\Manager as DB; use Illuminate\Database\Query\Builder; use Illuminate\Database\Query\JoinClause; +use Illuminate\Support\Str; use InvalidArgumentException; use PDOException; -use function substr_compare; /** * Provide an interface to the wt_gedcom table. @@ -741,12 +741,12 @@ class Tree */ public function createRecord(string $gedcom): GedcomRecord { - if (substr_compare($gedcom, '0 @@', 0, 4) !== 0) { + if (!Str::startsWith($gedcom, '0 @@ ')) { throw new InvalidArgumentException('GedcomRecord::createRecord(' . $gedcom . ') does not begin 0 @@'); } $xref = $this->getNewXref(); - $gedcom = '0 @' . $xref . '@' . substr($gedcom, 4); + $gedcom = '0 @' . $xref . '@ ' . Str::after($gedcom, '0 @@ '); // Create a change record $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->getUserName(); @@ -780,12 +780,12 @@ class Tree */ public function createFamily(string $gedcom): GedcomRecord { - if (substr_compare($gedcom, '0 @@ FAM', 0, 8) !== 0) { + if (!Str::startsWith($gedcom, '0 @@ FAM')) { throw new InvalidArgumentException('GedcomRecord::createFamily(' . $gedcom . ') does not begin 0 @@ FAM'); } $xref = $this->getNewXref(); - $gedcom = '0 @' . $xref . '@' . substr($gedcom, 4); + $gedcom = '0 @' . $xref . '@ FAM' . Str::after($gedcom, '0 @@ FAM'); // Create a change record $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->getUserName(); @@ -819,12 +819,12 @@ class Tree */ public function createIndividual(string $gedcom): GedcomRecord { - if (substr_compare($gedcom, '0 @@ INDI', 0, 9) !== 0) { + if (!Str::startsWith($gedcom, '0 @@ INDI')) { throw new InvalidArgumentException('GedcomRecord::createIndividual(' . $gedcom . ') does not begin 0 @@ INDI'); } $xref = $this->getNewXref(); - $gedcom = '0 @' . $xref . '@' . substr($gedcom, 4); + $gedcom = '0 @' . $xref . '@ INDI' . Str::after($gedcom, '0 @@ INDI'); // Create a change record $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->getUserName(); @@ -858,12 +858,12 @@ class Tree */ public function createMediaObject(string $gedcom): Media { - if (substr_compare($gedcom, '0 @@ OBJE', 0, 9) !== 0) { + if (!Str::startsWith($gedcom, '0 @@ OBJE')) { throw new InvalidArgumentException('GedcomRecord::createIndividual(' . $gedcom . ') does not begin 0 @@ OBJE'); } $xref = $this->getNewXref(); - $gedcom = '0 @' . $xref . '@' . substr($gedcom, 4); + $gedcom = '0 @' . $xref . '@ OBJE' . Str::after($gedcom, '0 @@ OBJE'); // Create a change record $gedcom .= "\n1 CHAN\n2 DATE " . date('d M Y') . "\n3 TIME " . date('H:i:s') . "\n2 _WT_USER " . Auth::user()->getUserName(); @@ -906,7 +906,7 @@ class Tree $individual = Individual::getInstance($this->getUserPreference($user, 'gedcomid'), $this); } - if ($individual === null) { + if ($individual === null && $this->getPreference('PEDIGREE_ROOT_ID') !== '') { $individual = Individual::getInstance($this->getPreference('PEDIGREE_ROOT_ID'), $this); } if ($individual === null) { diff --git a/tests/TestCase.php b/tests/TestCase.php index 06ef504869..298089f79a 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -87,6 +87,8 @@ class TestCase extends \PHPUnit\Framework\TestCase Site::$preferences = []; User::$cache = []; Tree::$trees = []; + GedcomRecord::$gedcom_record_cache = null; + GedcomRecord::$pending_record_cache = null; Auth::logout(); } diff --git a/tests/app/TreeTest.php b/tests/app/TreeTest.php index 0935b039d0..8ebef34517 100644 --- a/tests/app/TreeTest.php +++ b/tests/app/TreeTest.php @@ -204,6 +204,42 @@ class TreeTest extends \Fisharebest\Webtrees\TestCase } /** + * @covers \Fisharebest\Webtrees\Tree::createRecord + * @expectedException \InvalidArgumentException + * + * @return void + */ + public function testCreateInvalidRecord(): void + { + $tree = Tree::create('tree-name', 'Tree title'); + $user = User::create('user', 'User', 'user@example.com', 'secret'); + $user->setPreference('canadmin', '1'); + Auth::login($user); + + $tree->createRecord("0 @@FOO\n1 NOTE noted"); + } + + /** + * @covers \Fisharebest\Webtrees\Tree::createRecord + * + * @return void + */ + public function testCreateRecord(): void + { + $tree = Tree::create('tree-name', 'Tree title'); + $user = User::create('user', 'User', 'user@example.com', 'secret'); + $user->setPreference('canadmin', '1'); + Auth::login($user); + + $record = $tree->createRecord("0 @@ FOO\n1 NOTE noted"); + $this->assertTrue($record->isPendingAddition()); + + $user->setPreference('auto_accept', '1'); + $record = $tree->createRecord("0 @@ FOO\n1 NOTE noted"); + $this->assertFalse($record->isPendingAddition()); + } + + /** * @covers \Fisharebest\Webtrees\Tree::significantIndividual * * @return void @@ -214,7 +250,6 @@ class TreeTest extends \Fisharebest\Webtrees\TestCase $user = User::create('user', 'User', 'user@example.com', 'secret'); $user->setPreference('auto_accept', '1'); Auth::login($user); - $individual = $tree->significantIndividual($user); // No individuals in tree? Dummy individual $this->assertSame('I', $tree->significantIndividual($user)->xref()); |
