@@ -1041,6 +1041,55 @@ describe('Entries module', () => {
10411041
10421042 expect ( result ) . to . be . an ( 'array' ) ; // Should return array of missing references
10431043 } ) ;
1044+
1045+ fancy
1046+ . stdout ( { print : process . env . PRINT === 'true' || false } )
1047+ . it ( 'should flag reference when ref entry has wrong content type (ct2 ref when reference_to is ct1)' , ( ) => {
1048+ const ctInstance = new Entries ( constructorParam ) ;
1049+ ( ctInstance as any ) . currentUid = 'test-entry' ;
1050+ ( ctInstance as any ) . entryMetaData = [ { uid : 'blt123' , ctUid : 'ct2' } ] ; // Entry exists but is ct2
1051+
1052+ const referenceFieldSchema = { uid : 'ref' , display_name : 'Ref' , data_type : 'reference' , reference_to : [ 'ct1' ] } ;
1053+ const entryData = [ { uid : 'blt123' , _content_type_uid : 'ct2' } ] ;
1054+ const tree = [ { uid : 'test-entry' , name : 'Test Entry' } ] ;
1055+
1056+ const result = ctInstance . validateReferenceValues ( tree , referenceFieldSchema as any , entryData ) ;
1057+
1058+ expect ( result ) . to . have . length ( 1 ) ;
1059+ expect ( result [ 0 ] . missingRefs ) . to . deep . include ( { uid : 'blt123' , _content_type_uid : 'ct2' } ) ;
1060+ } ) ;
1061+
1062+ fancy
1063+ . stdout ( { print : process . env . PRINT === 'true' || false } )
1064+ . it ( 'should not flag reference when ref entry has correct content type (ct1 ref when reference_to is ct1)' , ( ) => {
1065+ const ctInstance = new Entries ( constructorParam ) ;
1066+ ( ctInstance as any ) . currentUid = 'test-entry' ;
1067+ ( ctInstance as any ) . entryMetaData = [ { uid : 'blt123' , ctUid : 'ct1' } ] ;
1068+
1069+ const referenceFieldSchema = { uid : 'ref' , display_name : 'Ref' , data_type : 'reference' , reference_to : [ 'ct1' ] } ;
1070+ const entryData = [ { uid : 'blt123' , _content_type_uid : 'ct1' } ] ;
1071+ const tree = [ { uid : 'test-entry' , name : 'Test Entry' } ] ;
1072+
1073+ const result = ctInstance . validateReferenceValues ( tree , referenceFieldSchema as any , entryData ) ;
1074+
1075+ expect ( result ) . to . have . length ( 0 ) ;
1076+ } ) ;
1077+
1078+ fancy
1079+ . stdout ( { print : process . env . PRINT === 'true' || false } )
1080+ . it ( 'should normalize reference_to string and allow matching ref (ct1 when reference_to is string ct1)' , ( ) => {
1081+ const ctInstance = new Entries ( constructorParam ) ;
1082+ ( ctInstance as any ) . currentUid = 'test-entry' ;
1083+ ( ctInstance as any ) . entryMetaData = [ { uid : 'blt456' , ctUid : 'ct1' } ] ;
1084+
1085+ const referenceFieldSchema = { uid : 'ref' , display_name : 'Ref' , data_type : 'reference' , reference_to : 'ct1' } ;
1086+ const entryData = [ { uid : 'blt456' , _content_type_uid : 'ct1' } ] ;
1087+ const tree = [ { uid : 'test-entry' , name : 'Test Entry' } ] ;
1088+
1089+ const result = ctInstance . validateReferenceValues ( tree , referenceFieldSchema as any , entryData ) ;
1090+
1091+ expect ( result ) . to . have . length ( 0 ) ;
1092+ } ) ;
10441093 } ) ;
10451094
10461095 describe ( 'validateModularBlocksField method' , ( ) => {
@@ -1365,5 +1414,130 @@ describe('Entries module', () => {
13651414
13661415 // Should not throw - method is void
13671416 } ) ;
1417+
1418+ fancy
1419+ . stdout ( { print : process . env . PRINT === 'true' || false } )
1420+ . it ( 'should flag JSON RTE embed when ref has wrong content type (ct2 when reference_to is ct1,sys_assets)' , ( ) => {
1421+ const ctInstance = new Entries ( constructorParam ) ;
1422+ ( ctInstance as any ) . currentUid = 'test-entry' ;
1423+ ( ctInstance as any ) . missingRefs = { 'test-entry' : [ ] } ;
1424+ ( ctInstance as any ) . entryMetaData = [ { uid : 'blt123' , ctUid : 'ct2' } ] ;
1425+
1426+ const schema = {
1427+ uid : 'json_rte' ,
1428+ display_name : 'JSON RTE' ,
1429+ data_type : 'richtext' ,
1430+ reference_to : [ 'ct1' , 'sys_assets' ] ,
1431+ } ;
1432+ const child = {
1433+ type : 'embed' ,
1434+ uid : 'child-uid' ,
1435+ attrs : { 'entry-uid' : 'blt123' , 'content-type-uid' : 'ct2' } ,
1436+ children : [ ] ,
1437+ } ;
1438+ const tree : Record < string , unknown > [ ] = [ ] ;
1439+
1440+ ( ctInstance as any ) . jsonRefCheck ( tree , schema , child ) ;
1441+
1442+ expect ( ( ctInstance as any ) . missingRefs [ 'test-entry' ] ) . to . have . length ( 1 ) ;
1443+ expect ( ( ctInstance as any ) . missingRefs [ 'test-entry' ] [ 0 ] . missingRefs ) . to . deep . include ( {
1444+ uid : 'blt123' ,
1445+ 'content-type-uid' : 'ct2' ,
1446+ } ) ;
1447+ } ) ;
1448+ } ) ;
1449+
1450+ describe ( 'fixMissingReferences method' , ( ) => {
1451+ fancy
1452+ . stdout ( { print : process . env . PRINT === 'true' || false } )
1453+ . it ( 'should filter out ref when ref has wrong content type (ct2 when reference_to is ct1)' , ( ) => {
1454+ const ctInstance = new Entries ( { ...constructorParam , fix : true } ) ;
1455+ ( ctInstance as any ) . currentUid = 'test-entry' ;
1456+ ( ctInstance as any ) . missingRefs = { 'test-entry' : [ ] } ;
1457+ ( ctInstance as any ) . entryMetaData = [ { uid : 'blt123' , ctUid : 'ct2' } ] ;
1458+
1459+ const field = {
1460+ uid : 'ref_field' ,
1461+ display_name : 'Ref' ,
1462+ data_type : 'reference' ,
1463+ reference_to : [ 'ct1' ] ,
1464+ } ;
1465+ const entry = [ { uid : 'blt123' , _content_type_uid : 'ct2' } ] ;
1466+ const tree = [ { uid : 'test-entry' , name : 'Test Entry' } ] ;
1467+
1468+ const result = ctInstance . fixMissingReferences ( tree , field as any , entry ) ;
1469+
1470+ expect ( result ) . to . have . length ( 0 ) ;
1471+ expect ( ( ctInstance as any ) . missingRefs [ 'test-entry' ] ) . to . have . length ( 1 ) ;
1472+ expect ( ( ctInstance as any ) . missingRefs [ 'test-entry' ] [ 0 ] . missingRefs ) . to . deep . include ( {
1473+ uid : 'blt123' ,
1474+ _content_type_uid : 'ct2' ,
1475+ } ) ;
1476+ } ) ;
1477+ } ) ;
1478+
1479+ describe ( 'jsonRefCheck in fix mode' , ( ) => {
1480+ fancy
1481+ . stdout ( { print : process . env . PRINT === 'true' || false } )
1482+ . it ( 'should return null when ref has wrong content type (fix mode)' , ( ) => {
1483+ const ctInstance = new Entries ( { ...constructorParam , fix : true } ) ;
1484+ ( ctInstance as any ) . currentUid = 'test-entry' ;
1485+ ( ctInstance as any ) . missingRefs = { 'test-entry' : [ ] } ;
1486+ ( ctInstance as any ) . entryMetaData = [ { uid : 'blt123' , ctUid : 'ct2' } ] ;
1487+
1488+ const schema = {
1489+ uid : 'json_rte' ,
1490+ display_name : 'JSON RTE' ,
1491+ data_type : 'richtext' ,
1492+ reference_to : [ 'ct1' ] ,
1493+ } ;
1494+ const child = {
1495+ type : 'embed' ,
1496+ uid : 'child-uid' ,
1497+ attrs : { 'entry-uid' : 'blt123' , 'content-type-uid' : 'ct2' } ,
1498+ children : [ ] ,
1499+ } ;
1500+ const tree : Record < string , unknown > [ ] = [ ] ;
1501+
1502+ const result = ( ctInstance as any ) . jsonRefCheck ( tree , schema , child ) ;
1503+
1504+ expect ( result ) . to . be . null ;
1505+ } ) ;
1506+ } ) ;
1507+
1508+ describe ( 'isRefContentTypeAllowed helper' , ( ) => {
1509+ const callHelper = ( refCtUid : string | undefined , referenceTo : string | string [ ] | undefined ) => {
1510+ const ctInstance = new Entries ( constructorParam ) ;
1511+ return ( ctInstance as any ) . isRefContentTypeAllowed ( refCtUid , referenceTo ) ;
1512+ } ;
1513+
1514+ fancy . stdout ( { print : process . env . PRINT === 'true' || false } ) . it ( 'returns true when refCtUid is in reference_to' , ( ) => {
1515+ expect ( callHelper ( 'ct1' , [ 'ct1' , 'ct2' ] ) ) . to . be . true ;
1516+ } ) ;
1517+
1518+ fancy . stdout ( { print : process . env . PRINT === 'true' || false } ) . it ( 'returns false when refCtUid is not in reference_to' , ( ) => {
1519+ expect ( callHelper ( 'ct2' , [ 'ct1' ] ) ) . to . be . false ;
1520+ } ) ;
1521+
1522+ fancy . stdout ( { print : process . env . PRINT === 'true' || false } ) . it ( 'returns true when reference_to is undefined' , ( ) => {
1523+ expect ( callHelper ( 'ct1' , undefined ) ) . to . be . true ;
1524+ } ) ;
1525+
1526+ fancy . stdout ( { print : process . env . PRINT === 'true' || false } ) . it ( 'normalizes reference_to string and allows matching refCtUid' , ( ) => {
1527+ expect ( callHelper ( 'ct1' , 'ct1' ) ) . to . be . true ;
1528+ expect ( callHelper ( 'ct2' , 'ct1' ) ) . to . be . false ;
1529+ } ) ;
1530+
1531+ fancy . stdout ( { print : process . env . PRINT === 'true' || false } ) . it ( 'returns false when reference_to is empty array' , ( ) => {
1532+ expect ( callHelper ( 'ct1' , [ ] ) ) . to . be . false ;
1533+ } ) ;
1534+
1535+ fancy . stdout ( { print : process . env . PRINT === 'true' || false } ) . it ( 'returns true when refCtUid is undefined' , ( ) => {
1536+ expect ( callHelper ( undefined , [ 'ct1' ] ) ) . to . be . true ;
1537+ } ) ;
1538+
1539+ fancy . stdout ( { print : process . env . PRINT === 'true' || false } ) . it ( 'returns true when refCtUid is in skipRefs' , ( ) => {
1540+ expect ( callHelper ( 'sys_assets' , [ 'ct1' ] ) ) . to . be . true ;
1541+ } ) ;
13681542 } ) ;
13691543} ) ;
0 commit comments